常量的 C# 命名约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/242534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:30:29  来源:igfitidea点击:

C# naming convention for constants?

c#naming-conventionsconst

提问by mmiika

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?

我个人认为对于现代 IDE,我们应该使用 camelCase,因为 ALL_CAPS 看起来很奇怪。你怎么认为?

采纳答案by Greg Beech

The recommended naming and capitalization convention is to use PascalCasingfor constants (Microsoft has a tool named StyleCopthat documents all the preferred conventions and can check your source for compliance - though it is a little bit tooanally retentive for many people's tastes). e.g.

推荐的命名和大小写约定使用PASCAL ÇASING常量(微软有一个名为工具了StyleCop该文档中的所有最佳惯例,并可以检查您的来源为合规性-尽管这是一个有点肛门保持了很多人的口味) . 例如

private const int TheAnswer = 42;

The Pascal capitalization convention is also documented in Microsoft's Framework Design Guidelines.

Pascal 大小写约定也记录在 Microsoft 的框架设计指南中

回答by dove

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.

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

回答by John

The ALL_CAPS is taken from the C and C++ way of working I believe. This article hereexplains how the style differences came about.

我相信 ALL_CAPS 取自 C 和 C++ 的工作方式。这篇文章在这里解释了风格差异是如何产生的。

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 FxCopand Microsoft StyleCopsoftware will help give you guidelines and check your code so everyone works the same way.

FxCop的微软了StyleCop软件将帮助给你指导和检查你的代码,以便每个人都以同样的方式。

回答by Treb

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 smellshungarian is not really a valid argument, IMO. The question should always be: Does it help, or does it hurt?

IMO,那种闻起来有匈牙利味道的东西并不是一个真正有效的论点。问题应该始终是:它有帮助,还是有害?

There are cases when hungarian helps. Not that many nowadays, but they still exist.

在某些情况下,匈牙利语会有所帮助。现在不是很多,但它们仍然存在。

回答by bh213

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 是决定命名约定的最佳方式 - 因此您的代码不会显得格格不入。

回答by Marc Gravell

I actually tend to prefer PascalCase here - but out of habit, I'm guilty of UPPER_CASE...

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

回答by user31939

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_notationhttp://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. http://en.wikibooks.org/wiki/C_Sharp_Programming/Naming

此处不鼓励使用大写字母:Pascal Case 是可接受的约定和 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

Microsoft 还在此处声明,如果要匹配现有方案,则可以使用 UPPERCASE。 http://msdn.microsoft.com/en-us/library/x2dbyw72.aspx

This pretty much sums it up.

这几乎总结了它。

回答by DavidRR

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 appearsthat Microsoft is recommending the use of camelCasing. But note that these constants are defined locally.

因此,对于常量,Microsoft似乎建议使用camelCasing. 但请注意,这些常量是在本地定义的。

Arguably, the naming of externally-visible constants is of greater interest. In practice, Microsoft documents its public constantsin the .NET class library as fields. Here are some examples:

可以说,外部可见常量的命名更受关注。实际上,Microsoft在 .NET 类库中将其公共常量记录为fields。这里有些例子:

The first two are examples of PascalCasing. The third appears to follow Microsoft's Capitalization Conventionsfor a two-letter acronym (although piis 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 as E(which represents the mathematical constant e).

前两个是PascalCasing. 第三个似乎遵循 Microsoft 的大写约定,使用两个字母的首字母缩写词(尽管pi不是首字母缩写词)。第四个似乎表明两个字母首字母缩略词的规则扩展到单个字母首字母缩略词或标识符,例如E(表示数学常数e)。

Furthermore, in its Capitalization Conventions document, Microsoft very directly states that field identifiers should be named via PascalCasingand gives the following examples for MessageQueue.InfiniteTimeoutand UInt32.Min:

此外,在其大写约定文档中,Microsoft 非常直接地声明字段标识符应命名为 viaPascalCasing并为MessageQueue.InfiniteTimeoutUInt32.Min提供以下示例:

public class MessageQueue
{
    public static readonly TimeSpan InfiniteTimeout;
}

public struct UInt32
{
    public const Min = 0;
}

Conclusion: Use PascalCasingfor public constants(which are documented as constor static readonlyfields).

结论:PascalCasing用于公共常量(记录为conststatic readonly字段)。

Finally, as far as I know, Microsoft does not advocate specific naming or capitalization conventions for privateidentifiers as shown in the examples presented in the question.

最后,据我所知,Microsoft 不提倡私有标识符的特定命名或大写约定,如问题中提供的示例所示。

回答by usefulBee

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;
 }