c#静态字段命名约定是什么?

时间:2022-09-02 09:57:29

I have recently started using ReSharper which is a fantastic tool. Today I came across a naming rule for static fields, namely prefixing with an underscore ie.

我最近开始使用ReSharper,这是一个很棒的工具。今天我遇到了静态字段的命名规则,即以下划线ie为前缀。

private static string _myString;
  1. Is this really the standard way to name static variables? If so is it just personal preference and style, or does it have some sort of lower level impact? Eg Compilation JIT etc?
  2. 这真的是命名静态变量的标准方法吗?如果是这样的话,这只是个人喜好和风格,还是有某种低级别的影响?如JIT编译等等?
  3. Where does this style originate from? I have always associated it with C++, is that correct?
  4. 这种风格起源于哪里?我一直把它和c++联系在一起,对吗?

8 个解决方案

#1


21  

The Microsoft guidelines are silent about private fields, they are only concerned with publicly visible members.

微软的指导方针对私有字段保持沉默,他们只关心公开可见的成员。

Common conventions are camelCase, _camelCase and even sometimes the hangover from C++/MFC m_camelCase.

常见的约定是camelCase、_camelCase,有时甚至是c++ /MFC m_camelCase的遗留问题。

If you use camelCase without a prefix, your property backing fields will differ from the property name only in case, which is not a problem in C#, but won't work in a case-insensitive language like VB.NET.

如果您使用没有前缀的camelCase,您的属性支持字段只会与属性名不同,这在c#中不是一个问题,但是在不区分大小写的语言(如VB.NET)中是行不通的。

So many people, including myself, like to use an underscore prefix so that the same standards can be used in all languages. In my experience, underscore is much more common than m_.

很多人,包括我自己,喜欢使用下划线前缀,这样所有语言都可以使用相同的标准。根据我的经验,下划线比m_更常见。

#2


17  

According to MSDN, use Pascal Case for static fields. I always chuckle when MSDN and StyleCop contradict each other :).

根据MSDN,在静态字段中使用Pascal案例。当MSDN和StyleCop互相矛盾的时候,我总是会笑。

So if you are following MSDN standards, the correct way is:

因此,如果您遵循MSDN标准,正确的方法是:

private static string MyString;

#3


11  

It's actually the style for a private field, static or not. (At least in ReSharper)

它实际上是私有字段的样式,不管静态与否。(至少在ReSharper)

#4


11  

According to StyleCop (and with the default settings), the correct way to name most fields (as specified below) is with a lowercase letter at the start.

根据StyleCop(和默认设置),正确的命名大多数字段(如下所指定)的方式是在开始时使用小写字母。

SA1306: FieldNamesMustBeginWithLowerCaseLetter

... Field and variable names must begin with a lower-case letter, unless the field is public or internal, const, or non-private and readonly. In these cases, the field should begin with an upper-case letter.

…字段和变量名必须以小写字母开头,除非字段是公共的或内部的、const的或非私有的和只读的。在这些情况下,字段应该以大写字母开头。

See also SA1309: FieldNamesMustNotBeginWithUnderscore.

参见SA1309:FieldNamesMustNotBeginWithUnderscore。

#5


3  

The problem I have with the MSDN guideline (use Pascal case) is that it results in there being no distinction between a private static variable and a public (non-static) property. The same issue would arise for static properties - no distinction between static and non-static.

我对MSDN指南(用Pascal案例)的问题是,它导致私有静态变量和公共(非静态)属性之间没有区别。对于静态属性也会出现同样的问题——静态属性和非静态属性没有区别。

Perhaps this is deliberate?

也许这是故意的吗?

One way out of this would be to use the same standard for statics as for non-statics but to always qualify use of statics by prefixing with the class name. It may be a few extra characters to type, but it makes the code more readable. For example:

解决这个问题的一种方法是对静态的使用与对非静态的使用相同的标准,但是总是通过在类名前面加上前缀来限定静态的使用。它可能需要输入一些额外的字符,但是它可以使代码更具可读性。例如:

public class Employee
{
    private static Int32 thresholdPercentage = 5;
    public static String TooMuchMessage = "Unacceptable pay rise - sorry!";

    private Decimal _salary = 0.0m;

    public void RaiseSalary(Int32 raiseAmountPercentage)  
    {
        if (raiseAmountPercentage > Employee.thresholdPercentage)
            throw new ApplicationException(Employee.TooMuchMessage);

        _salary *= 1 + (raiseAmountPercentage / 100);

        return;
    }
}

#6


3  

The convention is whatever your company's coding standards says it is.

约定就是你公司的编码标准所说的。

#7


0  

1 - There is not definitive standard rule for variable names. There are C# compiler requirements for what's allowed and what's not allowed (ie, can't start with a number), but programming language style rules are generally left up to the programmers / organizations. ReSharper has pre-defined style rules; however, they are merely set up as defaults in a convention over configuration approach and are modifiable.

1 -变量名没有确定的标准规则。c#编译器对允许和不允许的内容有要求(例如,不能以数字开头),但是编程语言风格规则通常由程序员/组织决定。ReSharper有预定义的样式规则;然而,它们只是在配置方法的约定中设置为默认值,并且可以修改。

2 - You can take a look at this wikipedia article to see the history behind Camel Casing.

2 -你可以看看*上的这篇文章,看看骆驼罩背后的历史。

#8


0  

For static fields, I've settled on StaticUpperCamelCase (eg. StaticPersonCache) as it clearly sets it apart from an instance variable. This includes private static fields as well as static fields with other visibility modifiers.

对于静态字段,我选择了StaticUpperCamelCase。(StaticPersonCache)因为它清楚地将它与实例变量分开。这包括私有静态字段以及具有其他可见性修饰符的静态字段。

For static variables it's less of a concern to me to indicate the public/private visibility through the name than it is to indicate how the variable operates among instances. Also, since there are not (and really should not be) many Static variables the 'Hugarian-like' modifier is not applied often.

对于静态变量,我更关心的是通过名称表示公共/私有的可见性,而不是表示变量如何在实例之间操作。而且,由于没有(实际上不应该是)许多静态变量,“Hugarian-like”修饰符并不经常应用。

Similarly, for thread static variables ([ThreadStatic] or ThreadLocal) the convention I use is TS_UpperCamelCase (eg. TS_Random). Once again, this "break" from norms conveys very important information that other developers might fail to see on first glance. The name is thus used as a cautionary flag, of sorts.

类似地,对于线程静态变量([ThreadStatic]或ThreadLocal),我使用的约定是TS_UpperCamelCase(例如)。TS_Random)。同样,这种从规范的“突破”传达了非常重要的信息,其他开发人员第一眼可能看不到这些信息。因此,这个名字被用作某种警示标志。

I use ReSharper and have adjusted the warnings/hints accordingly; most other naming conventions are left at the ReSharper default settings.

我使用ReSharper并且相应地调整了警告/提示;大多数其他命名约定都保留在ReSharper默认设置中。

My selection of such "non standard" conventions for static/thread-static fields (note: Microsoft uses TS_ in some code in the .NET library) is because I've run into more than one 'quirk' due to misidentification of Static/ThreadStatic/Instance variables: that's much harder to do with StaticX, TS_X, _x.

对于静态/线程静态字段(注意:Microsoft在. net库中的一些代码中使用TS_),我之所以选择这种“非标准”约定,是因为由于对静态/线程静态/实例变量的错误识别,我遇到了不止一个“怪癖”:使用StaticX、TS_X和_x要困难得多。

#1


21  

The Microsoft guidelines are silent about private fields, they are only concerned with publicly visible members.

微软的指导方针对私有字段保持沉默,他们只关心公开可见的成员。

Common conventions are camelCase, _camelCase and even sometimes the hangover from C++/MFC m_camelCase.

常见的约定是camelCase、_camelCase,有时甚至是c++ /MFC m_camelCase的遗留问题。

If you use camelCase without a prefix, your property backing fields will differ from the property name only in case, which is not a problem in C#, but won't work in a case-insensitive language like VB.NET.

如果您使用没有前缀的camelCase,您的属性支持字段只会与属性名不同,这在c#中不是一个问题,但是在不区分大小写的语言(如VB.NET)中是行不通的。

So many people, including myself, like to use an underscore prefix so that the same standards can be used in all languages. In my experience, underscore is much more common than m_.

很多人,包括我自己,喜欢使用下划线前缀,这样所有语言都可以使用相同的标准。根据我的经验,下划线比m_更常见。

#2


17  

According to MSDN, use Pascal Case for static fields. I always chuckle when MSDN and StyleCop contradict each other :).

根据MSDN,在静态字段中使用Pascal案例。当MSDN和StyleCop互相矛盾的时候,我总是会笑。

So if you are following MSDN standards, the correct way is:

因此,如果您遵循MSDN标准,正确的方法是:

private static string MyString;

#3


11  

It's actually the style for a private field, static or not. (At least in ReSharper)

它实际上是私有字段的样式,不管静态与否。(至少在ReSharper)

#4


11  

According to StyleCop (and with the default settings), the correct way to name most fields (as specified below) is with a lowercase letter at the start.

根据StyleCop(和默认设置),正确的命名大多数字段(如下所指定)的方式是在开始时使用小写字母。

SA1306: FieldNamesMustBeginWithLowerCaseLetter

... Field and variable names must begin with a lower-case letter, unless the field is public or internal, const, or non-private and readonly. In these cases, the field should begin with an upper-case letter.

…字段和变量名必须以小写字母开头,除非字段是公共的或内部的、const的或非私有的和只读的。在这些情况下,字段应该以大写字母开头。

See also SA1309: FieldNamesMustNotBeginWithUnderscore.

参见SA1309:FieldNamesMustNotBeginWithUnderscore。

#5


3  

The problem I have with the MSDN guideline (use Pascal case) is that it results in there being no distinction between a private static variable and a public (non-static) property. The same issue would arise for static properties - no distinction between static and non-static.

我对MSDN指南(用Pascal案例)的问题是,它导致私有静态变量和公共(非静态)属性之间没有区别。对于静态属性也会出现同样的问题——静态属性和非静态属性没有区别。

Perhaps this is deliberate?

也许这是故意的吗?

One way out of this would be to use the same standard for statics as for non-statics but to always qualify use of statics by prefixing with the class name. It may be a few extra characters to type, but it makes the code more readable. For example:

解决这个问题的一种方法是对静态的使用与对非静态的使用相同的标准,但是总是通过在类名前面加上前缀来限定静态的使用。它可能需要输入一些额外的字符,但是它可以使代码更具可读性。例如:

public class Employee
{
    private static Int32 thresholdPercentage = 5;
    public static String TooMuchMessage = "Unacceptable pay rise - sorry!";

    private Decimal _salary = 0.0m;

    public void RaiseSalary(Int32 raiseAmountPercentage)  
    {
        if (raiseAmountPercentage > Employee.thresholdPercentage)
            throw new ApplicationException(Employee.TooMuchMessage);

        _salary *= 1 + (raiseAmountPercentage / 100);

        return;
    }
}

#6


3  

The convention is whatever your company's coding standards says it is.

约定就是你公司的编码标准所说的。

#7


0  

1 - There is not definitive standard rule for variable names. There are C# compiler requirements for what's allowed and what's not allowed (ie, can't start with a number), but programming language style rules are generally left up to the programmers / organizations. ReSharper has pre-defined style rules; however, they are merely set up as defaults in a convention over configuration approach and are modifiable.

1 -变量名没有确定的标准规则。c#编译器对允许和不允许的内容有要求(例如,不能以数字开头),但是编程语言风格规则通常由程序员/组织决定。ReSharper有预定义的样式规则;然而,它们只是在配置方法的约定中设置为默认值,并且可以修改。

2 - You can take a look at this wikipedia article to see the history behind Camel Casing.

2 -你可以看看*上的这篇文章,看看骆驼罩背后的历史。

#8


0  

For static fields, I've settled on StaticUpperCamelCase (eg. StaticPersonCache) as it clearly sets it apart from an instance variable. This includes private static fields as well as static fields with other visibility modifiers.

对于静态字段,我选择了StaticUpperCamelCase。(StaticPersonCache)因为它清楚地将它与实例变量分开。这包括私有静态字段以及具有其他可见性修饰符的静态字段。

For static variables it's less of a concern to me to indicate the public/private visibility through the name than it is to indicate how the variable operates among instances. Also, since there are not (and really should not be) many Static variables the 'Hugarian-like' modifier is not applied often.

对于静态变量,我更关心的是通过名称表示公共/私有的可见性,而不是表示变量如何在实例之间操作。而且,由于没有(实际上不应该是)许多静态变量,“Hugarian-like”修饰符并不经常应用。

Similarly, for thread static variables ([ThreadStatic] or ThreadLocal) the convention I use is TS_UpperCamelCase (eg. TS_Random). Once again, this "break" from norms conveys very important information that other developers might fail to see on first glance. The name is thus used as a cautionary flag, of sorts.

类似地,对于线程静态变量([ThreadStatic]或ThreadLocal),我使用的约定是TS_UpperCamelCase(例如)。TS_Random)。同样,这种从规范的“突破”传达了非常重要的信息,其他开发人员第一眼可能看不到这些信息。因此,这个名字被用作某种警示标志。

I use ReSharper and have adjusted the warnings/hints accordingly; most other naming conventions are left at the ReSharper default settings.

我使用ReSharper并且相应地调整了警告/提示;大多数其他命名约定都保留在ReSharper默认设置中。

My selection of such "non standard" conventions for static/thread-static fields (note: Microsoft uses TS_ in some code in the .NET library) is because I've run into more than one 'quirk' due to misidentification of Static/ThreadStatic/Instance variables: that's much harder to do with StaticX, TS_X, _x.

对于静态/线程静态字段(注意:Microsoft在. net库中的一些代码中使用TS_),我之所以选择这种“非标准”约定,是因为由于对静态/线程静态/实例变量的错误识别,我遇到了不止一个“怪癖”:使用StaticX、TS_X和_x要困难得多。