C# 常量的命名约定?
private const int THE_ANSWER = 42;
或者
private const int theAnswer = 42;
我个人认为,对于现代 IDE,我们应该使用驼峰命名法,因为 ALL_CAPS 看起来很奇怪。 你怎么认为?
private const int THE_ANSWER = 42;
or
private const int theAnswer = 42;
Personally I think with modern IDEs we should go with camelCase as ALL_CAPS looks strange. What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
建议的命名和大小写约定是使用 PascalCasing 用于常量(微软有一个名为 StyleCop 的工具,它记录了所有首选约定,并且可以检查您的遵守的来源 - 尽管对于许多人的口味来说,它有点太了)。 例如,
Pascal 大小写约定也记录在 Microsoft 的 框架设计指南中。
The recommended naming and capitalization convention is to use PascalCasing for constants (Microsoft has a tool named StyleCop that documents all the preferred conventions and can check your source for compliance - though it is a little bit too anally retentive for many people's tastes). e.g.
The Pascal capitalization convention is also documented in Microsoft's Framework Design Guidelines.
从视觉上看,大写是正确的选择。 这样就很容易被认出来了。
为了独特性和不留猜测的机会,我投票给 UPPER_CASE!
注意:当要在页面顶部的同一文件中使用常量并用于智能感知时,大写将很有用; 但是,如果要将它们移至独立类,则使用大写不会有太大区别,例如:
Visually, Upper Case is the way to go. It is so recognizable that way.
For the sake of uniqueness and leaving no chance for guessing, I vote for UPPER_CASE!
Note: The Upper Case will be useful when constants are to be used within the same file at the top of the page and for intellisense purposes; however, if they were to be moved to an independent class, using Upper Case would not make much difference, as an example:
实际上,至少
如果您查看 .NET 库,IMO 是决定命名约定的最佳方法 - 因此您的代码看起来不会不合适。
Actually, it is
At least if you look at the .NET library, which IMO is the best way to decide naming conventions - so your code doesn't look out of place.
我仍然使用大写的 const 值,但这更多的是出于习惯,而不是出于任何特定原因。
当然,它可以很容易地立即看出某些东西是 const。 我的问题是:我们真的需要这些信息吗? 它能以某种方式帮助我们避免错误吗? 如果我给 const 赋值,编译器会告诉我我做了一些愚蠢的事情。
我的结论是:选择骆驼肠衣。 也许我也会改变我的风格;-)
编辑:
匈牙利语的味道并不是真正有效的论点,IMO。 问题应该始终是:它有帮助还是有害?
在某些情况下,匈牙利语会有所帮助。 现在已经没有那么多了,但它们仍然存在。
I still go with the uppercase for const values, but this is more out of habit than for any particular reason.
Of course it makes it easy to see immediately that something is a const. The question to me is: Do we really need this information? Does it help us in any way to avoid errors? If I assign a value to the const, the compiler will tell me I did something dumb.
My conclusion: Go with the camel casing. Maybe I will change my style too ;-)
Edit:
That something smells hungarian is not really a valid argument, IMO. The question should always be: Does it help, or does it hurt?
There are cases when hungarian helps. Not that many nowadays, but they still exist.
Microsoft 在其文章常量(C# 编程指南)中给出了以下示例:
因此,对于常量,Microsoft 似乎建议使用
驼峰式
。 但请注意,这些常量是本地定义的。可以说,外部可见常量的命名更令人感兴趣。 实际上,Microsoft 将 .NET 类库中的公共常量记录为字段。 以下是一些示例:
静态只读
)前两个是
PascalCasing
。 第三个似乎遵循 Microsoft 的两个字母缩写词的大写约定 (虽然 pi 不是缩写词)。 第四个似乎表明两个字母缩写词的规则扩展到单个字母缩写词或标识符,例如E
(表示数学常数 e)。此外,在其大写约定文档中,Microsoft 非常直接地指出字段标识符应通过 PascalCasing 命名,并给出了以下示例 MessageQueue.InfiniteTimeout 和 UInt32.Min:
结论:使用
PascalCasing
用于公共常量(记录为const
或static readonly
字段)。最后,据我所知,微软不提倡对私有标识符进行特定的命名或大写约定,如问题中提供的示例所示。
In its article Constants (C# Programming Guide), Microsoft gives the following example:
So, for constants, it appears that Microsoft is recommending the use of
camelCasing
. But note that these constants are defined locally.Arguably, the naming of externally-visible constants is of greater interest. In practice, Microsoft documents its public constants in the .NET class library as fields. Here are some examples:
static readonly
)The first two are examples of
PascalCasing
. The third appears to follow Microsoft's Capitalization Conventions for a two-letter acronym (although pi is not an acryonym). And the fourth one seems to suggest that the rule for a two-letter acryonym extends to a single letter acronym or identifier such asE
(which represents the mathematical constant e).Furthermore, in its Capitalization Conventions document, Microsoft very directly states that field identifiers should be named via
PascalCasing
and gives the following examples for MessageQueue.InfiniteTimeout and UInt32.Min:Conclusion: Use
PascalCasing
for public constants (which are documented asconst
orstatic readonly
fields).Finally, as far as I know, Microsoft does not advocate specific naming or capitalization conventions for private identifiers as shown in the examples presented in the question.
首先,匈牙利表示法是使用前缀来显示参数的数据类型或预期用途的做法。
微软的命名约定对匈牙利表示法说不
http://en.wikipedia.org/wiki/Hungarian_notation
http://msdn.microsoft.com/en-us/library/ms229045.aspx
不鼓励使用大写字母,如下所述:
帕斯卡大小写是可接受的约定和尖叫大写。
http://en.wikibooks.org/wiki/C_Sharp_Programming/Naming
Microsoft 也在此处声明如果与现有方案匹配,则可以使用大写字母。
http://msdn.microsoft.com/en-us/library/x2dbyw72.aspx
这几乎概括了这一点。
First, Hungarian Notation is the practice of using a prefix to display a parameter's data type or intended use.
Microsoft's naming conventions for says no to Hungarian Notation
http://en.wikipedia.org/wiki/Hungarian_notation
http://msdn.microsoft.com/en-us/library/ms229045.aspx
Using UPPERCASE is not encouraged as stated here:
Pascal Case is the acceptable convention and SCREAMING CAPS.
http://en.wikibooks.org/wiki/C_Sharp_Programming/Naming
Microsoft also states here that UPPERCASE can be used if it is done to match the the existed scheme.
http://msdn.microsoft.com/en-us/library/x2dbyw72.aspx
This pretty much sums it up.
把匈牙利语留给匈牙利人吧。
在这个例子中,我什至会省略最终的文章,而只是选择“
这是答案还是那是答案?”
*按照帕斯卡严格正确的方式进行编辑,但我认为这个问题是在寻求更多生活的答案,宇宙和一切。
Leave Hungarian to the Hungarians.
In the example I'd even leave out the definitive article and just go with
Is that answer or is that the answer?
*Made edit as Pascal strictly correct, however I was thinking the question was seeking more of an answer to life, the universe and everything.
我实际上更喜欢这里的 PascalCase - 但出于习惯,我对 UPPER_CASE 感到内疚......
I actually tend to prefer PascalCase here - but out of habit, I'm guilty of UPPER_CASE...
我相信 ALL_CAPS 取自 C 和 C++ 的工作方式。 本文此处解释了如何风格差异就出现了。
在新的 IDE(例如 Visual Studio)中,很容易识别类型、范围以及它们是否恒定,因此这并不是绝对必要的。
FxCop 和 Microsoft StyleCop 软件将帮助您提供指导并检查您的代码,以便每个人都以相同的方式工作。
The ALL_CAPS is taken from the C and C++ way of working I believe. This article here explains how the style differences came about.
In the new IDE's such as Visual Studio it is easy to identify the types, scope and if they are constant so it is not strictly necessary.
The FxCop and Microsoft StyleCop software will help give you guidelines and check your code so everyone works the same way.