对标准标识符命名感到困惑

发布于 2024-10-15 20:41:39 字数 262 浏览 2 评论 0原文

读一本书,它说这两个是强制性约定:

  • 不要在标识符中使用下划线
  • 标识符不应以大写字母开头

但在很多地方,我可以看到,特别是在属性中,作者没有遵循这一点,例如:

private int x

public int X..

与下划线相同..有时他使用 _x 和 X 作为属性。

请问正确的命名规则是什么?

reading a book, it says that these two are obligate conventions:

  • do not use underscore in identifiers
  • identifier should not begin with capital letter

But in many places then I can see, especially in properties, that author does not follow that, e.g.:

private int x

and

public int X..

The same with underscores..sometimes he uses _x and X for properties.

What is the correct naming convention, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断桥再见 2024-10-22 20:41:39

对于所有标识符来说,规则并不相同。周围有多种标准,只要一致使用,所有标准都很好。

我从MS指南中记得:

  • 类(类型)名称和公共成员使用PascalCasing
  • 私有成员,本地变量和参数使用camelCasing

但是camelCasing只是意味着“不要开始”带有大写字母的”、field_fieldmFieldm_field 均符合条件。

The rules are not the same for all identifiers. And there are multiple standards around, all are good as long as they are used consistently.

What I remember from the MS guidelines:

  • class (type) names and public members use PascalCasing
  • private members, local vars and parameters use camelCasing

But camelCasing just means "don't start with a capital", field, _field, mField and m_field all qualify.

活泼老夫 2024-10-22 20:41:39

除了所有其他好的答案之外,我还要补充一点,以两个下划线开始标识符是一种不好的做法。 C# 规范规定,我们保留使以两个下划线开头的内容具有特殊含义的权利。这是我们的逃生舱口,因此如果我们确实需要,我们可以在不破坏任何人的情况下向该语言添加新功能。或者,我们可以使用以两个下划线开头的方法作为专用编译器生成的方法。

In addition to all the other fine answers I'll add that it is a bad practice to begin an identifier with two underbars. The C# specification says that we reserve the right to make things beginning with two underbars have special meanings. This is our escape hatch so that if we really need to, we can add new features to the language without breaking anyone. Or, we can use methods that begin with two underbars as special-purpose compiler generated methods.

携余温的黄昏 2024-10-22 20:41:39

正如其他人提到的,命名约定因开发人员和团队而异。唯一真正重要的是你要始终如一

如果您正在寻找可遵循的指南,Microsoft 发布了一组 .NET Framework 的通用命名约定。它们在其网站上有些分散,但此页面是好的开始。

执行摘要如下:

  • 您应该对所有公共成员、类型和命名空间名称使用 Pascal 大小写(例如 BackColor
  • 您应该对参数名称和局部变量使用驼峰大小写(例如 backColor )。

此外,您经常会看到以下划线前缀命名的私有变量(特别是那些具有相应公共属性的变量)(例如_backColor)。另一种约定是在前面加上 m_

As others have mentioned, naming conventions vary across developers and teams. The only really important thing is that you're consistent.

If you're looking for guidelines to follow, Microsoft publishes a set of General Naming Conventions for the .NET Framework. They're somewhat scattered around on their website, but this page is a good start.

The executive summary is as follows:

  • You should use Pascal casing for all public member, type, and namespace names (e.g. BackColor)
  • You should use camel casing for parameter names and local variables (e.g. backColor).

Additionally, you will often see private variables (particularly those that have a corresponding public property) named with an underscore prefix (e.g. _backColor). Another convention prepends an m_.

半暖夏伤 2024-10-22 20:41:39

这取决于品味或您所在的团队。

变量永远不会以大写字母或数字开头。
您可以使用下划线,这是许多开发人员首选的,因为他们的所有字段成员都通过智能感知在 Visual Studio 中分组在一起。

private int _number;
private int _Number;

他们中的大多数人更喜欢下划线和大写字母,因为在编写普通变量名时使用驼峰式命名法。

你可以这样写:

private int number;

但是因为你使用下划线,所以第一个字符是下划线,下一个字符将是大写(参见camelCaseNotation)
像这样:

private int _Number;

对于属性,您只需使用大写字母而不带前缀即可:

public int Number { get; set; }

关于参数,如果您愿意,可以使用与字段成员相同的下划线前缀:

private int _Number;

public MyClass(int _Number) {
  this._Number = _Number;
}

没有正确或错误,只需尝试遵循语言指南并适应自己与您一起工作的团队。与您的团队讨论您将使用什么,并且所有人都使用相同的内容,这使得代码更易于阅读。

关于 m_ 前缀表示法或匈牙利表示法 (sName, iNumber, ...) ,这有点“旧”,尽量避免这种情况(大多数 C/C++ 程序员使用 C# 中新的表示法)。

It depends on taste or the team that you are working in.

variables never start with a capital letter or a number.
You can use an underscore, which is preferred by many developers because all their field members are grouped together in visual studio by intellisense.

private int _number;
private int _Number;

Most of them prefer underscore and a capital letter because when writing a normal variable name you use camelCaseNotation.

You would write like this:

private int number;

but because you use an underscore the first char is an underscore and the next char would be uppercase (cf. camelCaseNotation)
like this:

private int _Number;

For properties you just write them with a capital letter without prefixes:

public int Number { get; set; }

About parameters you can use the same underscore prefix as field members if you like that:

private int _Number;

public MyClass(int _Number) {
  this._Number = _Number;
}

There is no right or wrong, just try to follow the language guidelines and adapt yourself to the team that you are working with. Discuss with your team what you will use and all use the same, which makes code much better to read.

About the m_ prefix notation or hungarian notation (sName, iNumber, ...) , this is a bit "old", try to avoid this (most C/C++-programmers use that which are new to C#).

故事和酒 2024-10-22 20:41:39

我不完全确定是否有一种正确的命名标识符的方法。有些人使用下划线来表示成员变量,有些人使用 m_ 前缀。有些使用下划线来表示作为参数传入的变量。

至于大写字母,有些使用它们,有些不使用它们,有些使用“驼峰式”,即第一个字母不是大写,但后续的“单词”是,例如:

thisIsCamelCase

重要的是你是一致的。如果您决定使用 _ 作为成员的前缀,请确保所有成员都遵循该约定。

就我个人而言,我使用 _ 作为参数的前缀,使用 m_ 作为成员的前缀。

public class Vector
{
    private float m_X;
    private float m_Y;
    private float m_Z;

    public Vector(float _x, float _y, float _z)
    {
        m_X = _x;
        m_Y = _y;
        m_Z = _z;
    } // eo ctor
} // eo class Vector

你的书宣扬“永远永远永远”,然后立即忽略了它自己的建议,这听起来确实很狡猾。

I'm not entirely sure that there is a correct way of naming identifiers. Some people use underscores to denote member variables, some use a m_ prefix. Some use an underscore to denote variables that have been passed in as parameters.

As for capital letters, some use them some don't and some use "camel-casing" which is where the first letter is non-capital, but subsequent "words" are, e.g:

thisIsCamelCase

What does matter, is that you are consistent. If you decide to use _ as a prefix for members, make sure all members have that convention.

Personally, I use _ as a prefix for parameters and m_ as a prefix for members.

public class Vector
{
    private float m_X;
    private float m_Y;
    private float m_Z;

    public Vector(float _x, float _y, float _z)
    {
        m_X = _x;
        m_Y = _y;
        m_Z = _z;
    } // eo ctor
} // eo class Vector

It definitely sounds dodgy that your book preaches this "never never never" then promptly ignores it's own advice.

ㄟ。诗瑗 2024-10-22 20:41:39

我想补充一点,常量仅使用大写字母是很常见的

public const int STATUS_OK = 0;

I would add that its common to use only capital letters for constants

public const int STATUS_OK = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文