简体   繁体   English

常量的 C# 命名约定?

[英]C# naming convention for constants?

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.我个人认为对于现代 IDE,我们应该使用 camelCase,因为 ALL_CAPS 看起来很奇怪。 What do you think?你怎么认为?

The recommended naming and capitalization convention is to use P ascal C asing 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).推荐的命名和大小写约定是对常量使用P ascal C asing (Microsoft 有一个名为StyleCop的工具,它记录了所有首选约定,并可以检查您的来源是否合规 - 尽管对许多人的口味来说它有点过于保留) . eg例如

private const int TheAnswer = 42;

The Pascal capitalization convention is also documented in Microsoft's Framework Design Guidelines . Pascal 大小写约定也记录在 Microsoft 的框架设计指南中

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!为了唯一性,不留任何猜测的机会,我投票给 UPPER_CASE!

const int THE_ANSWER = 42;

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:但是,如果将它们移动到一个独立的类,使用大写不会有太大区别,例如:

public static class Constant
{
    public static readonly int Cons1 = 1;
    public static readonly int coNs2 = 2;
    public static readonly int cOns3 = 3;
    public static readonly int CONS4 = 4;
}

// Call constants from anywhere
// Since the class has a unique and recognizable name, Upper Case might lose its charm
private void DoSomething(){
var getCons1 = Constant.Cons1;
var getCons2 = Constant.coNs2;
var getCons3 = Constant.cOns3;
var getCons4 = Constant.CONS4;
 }

Actually, it is实际上,它是

private const int TheAnswer = 42;

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.至少如果您查看 .NET 库,IMO 是决定命名约定的最佳方式 - 因此您的代码不会显得格格不入。

I still go with the uppercase for const values, but this is more out of habit than for any particular reason.对于 const 值,我仍然使用大写字母,但这更多是出于习惯而不是出于任何特殊原因。

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.如果我为 const 赋值,编译器会告诉我我做了一些愚蠢的事情。

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. 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.现在不是很多,但它们仍然存在。

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微软的命名约定对匈牙利符号说不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.此处不鼓励使用大写字母:Pascal Case 是可接受的约定和 SCREAMING CAPS。 http://en.wikibooks.org/wiki/C_Sharp_Programming/Naming 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. Microsoft 还在此处声明,如果要匹配现有方案,则可以使用 UPPERCASE。 http://msdn.microsoft.com/en-us/library/x2dbyw72.aspx http://msdn.microsoft.com/en-us/library/x2dbyw72.aspx

This pretty much sums it up.这几乎总结了它。

In its article Constants (C# Programming Guide) , Microsoft gives the following example: Microsoft 在其文章Constants (C# Programming Guide) 中给出了以下示例:

class Calendar3
{
    const int months = 12;
    const int weeks = 52;
    const int days = 365;

    const double daysPerWeek = (double) days / (double) weeks;
    const double daysPerMonth = (double) days / (double) months;
}

So, for constants, it appears that Microsoft is recommending the use of camelCasing .因此,对于常量,Microsoft似乎建议使用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 .实际上,Microsoft 在 .NET 类库中将其公共常量记录为fields Here are some examples:这里有些例子:

The first two are examples of PascalCasing .前两个是PascalCasing例子。 The third appears to follow Microsoft's Capitalization Conventions for a two-letter acronym (although pi is not an acryonym).第三个似乎遵循 Microsoft 的大写约定,使用两个字母的首字母缩写词(尽管pi不是首字母缩写词)。 And the fourth one seems to suggest that the rule for a two-letter acryonym extends to a single letter acronym or identifier such as E (which represents the mathematical constant e ).第四个似乎表明两个字母首字母缩略词的规则扩展到单个字母首字母缩略词或标识符,例如E (代表数学常数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 :此外,在其大小写约定文档中,Microsoft 非常直接地声明字段标识符应通过PascalCasing命名,并为MessageQueue.InfiniteTimeoutUInt32.Min提供以下示例:

public class MessageQueue
{
    public static readonly TimeSpan InfiniteTimeout;
}

public struct UInt32
{
    public const Min = 0;
}

Conclusion: Use PascalCasing for public constants (which are documented as const or static readonly fields).结论:将PascalCasing用于公共常量(记录为conststatic readonly字段)。

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.最后,据我所知,Microsoft 不提倡私有标识符的特定命名或大写约定,如问题中提供的示例所示。

Leave Hungarian to the Hungarians.把匈牙利语留给匈牙利人。

In the example I'd even leave out the definitive article and just go with在这个例子中,我什至省略了权威性的文章,直接使用

private const int Answer = 42;

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 . *严格按照帕斯卡进行编辑,但我认为这个问题是在寻求更多关于 生命、宇宙和一切的答案。

The ALL_CAPS is taken from the C and C++ way of working I believe.我相信 ALL_CAPS 取自 C 和 C++ 的工作方式。 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.在诸如 Visual Studio 之类的新 IDE 中,很容易识别类型、范围以及它们是否是常量,因此不是绝对必要的。

The FxCop and Microsoft StyleCop software will help give you guidelines and check your code so everyone works the same way. FxCop和 Microsoft StyleCop软件将帮助您提供指导并检查您的代码,以便每个人都以相同的方式工作。

我实际上倾向于在这里更喜欢 PascalCase - 但出于习惯,我对 UPPER_CASE 感到内疚......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM