对标准标识符命名感到困惑
读一本书,它说这两个是强制性约定:
- 不要在标识符中使用下划线
- 标识符不应以大写字母开头
但在很多地方,我可以看到,特别是在属性中,作者没有遵循这一点,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于所有标识符来说,规则并不相同。周围有多种标准,只要一致使用,所有标准都很好。
我从MS指南中记得:
但是camelCasing只是意味着“不要开始”带有大写字母的”、
field
、_field
、mField
和m_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:
But camelCasing just means "don't start with a capital",
field
,_field
,mField
andm_field
all qualify.除了所有其他好的答案之外,我还要补充一点,以两个下划线开始标识符是一种不好的做法。 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.
正如其他人提到的,命名约定因开发人员和团队而异。唯一真正重要的是你要始终如一。
如果您正在寻找可遵循的指南,Microsoft 发布了一组 .NET Framework 的通用命名约定。它们在其网站上有些分散,但此页面是好的开始。
执行摘要如下:
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:
BackColor
)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 anm_
.这取决于品味或您所在的团队。
变量永远不会以大写字母或数字开头。
您可以使用下划线,这是许多开发人员首选的,因为他们的所有字段成员都通过智能感知在 Visual Studio 中分组在一起。
他们中的大多数人更喜欢下划线和大写字母,因为在编写普通变量名时使用驼峰式命名法。
你可以这样写:
但是因为你使用下划线,所以第一个字符是下划线,下一个字符将是大写(参见camelCaseNotation)
像这样:
对于属性,您只需使用大写字母而不带前缀即可:
关于参数,如果您愿意,可以使用与字段成员相同的下划线前缀:
没有正确或错误,只需尝试遵循语言指南并适应自己与您一起工作的团队。与您的团队讨论您将使用什么,并且所有人都使用相同的内容,这使得代码更易于阅读。
关于 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.
Most of them prefer underscore and a capital letter because when writing a normal variable name you use camelCaseNotation.
You would write like this:
but because you use an underscore the first char is an underscore and the next char would be uppercase (cf. camelCaseNotation)
like this:
For properties you just write them with a capital letter without prefixes:
About parameters you can use the same underscore prefix as field members if you like that:
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#).
我不完全确定是否有一种正确的命名标识符的方法。有些人使用下划线来表示成员变量,有些人使用
m_
前缀。有些使用下划线来表示作为参数传入的变量。至于大写字母,有些使用它们,有些不使用它们,有些使用“驼峰式”,即第一个字母不是大写,但后续的“单词”是,例如:
重要的是你是一致的。如果您决定使用
_
作为成员的前缀,请确保所有成员都遵循该约定。就我个人而言,我使用
_
作为参数的前缀,使用m_
作为成员的前缀。你的书宣扬“永远永远永远”,然后立即忽略了它自己的建议,这听起来确实很狡猾。
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:
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 andm_
as a prefix for members.It definitely sounds dodgy that your book preaches this "never never never" then promptly ignores it's own advice.
我想补充一点,常量仅使用大写字母是很常见的
I would add that its common to use only capital letters for constants