C# 中修饰符的顺序有约定吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/191929/
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 17:25:59  来源:igfitidea点击:

Is there a convention to the order of modifiers in C#?

c#

提问by CrashCodes

If I were to use more than one, what order should I use modifier keywords such as:

如果我要使用多个,我应该按什么顺序使用修饰符关键字,例如:

public, private, protected, virtual, abstract, override, new, static, internal, sealed, and any others I'm forgetting.

public, private, protected, virtual, abstract, override, new, static, internal, sealed, 以及我忘记的任何其他内容。

采纳答案by Mark Heath

StyleCopis available as a Visual Studio extensionor a NuGet packageand can validate your source code against the rules some teams in Microsoft use. StyleCop likes the access modifier to come first.

StyleCop可用作Visual Studio 扩展NuGet 包,并且可以根据 Microsoft 的某些团队使用的规则验证您的源代码。StyleCop 喜欢将访问修饰符放在首位。

EDIT: Microsoft isn't itself totally consistent; different teams use different styles. For example StyleCop suggests putting using directives in the namespace, but this is not followed in the Roslyn source code.

编辑:微软本身并不完全一致;不同的团队使用不同的风格。例如,StyleCop 建议在命名空间中放置 using 指令,但 Roslyn 源代码中并未遵循这一点。

回答by Chris Charabaruk

I usually start off with the access modifier first, then virtual/abstract/sealed, then override/new/etc. although others might do it differently. Almost invariably, the access modifier will be first, however.

我通常先从访问修饰符开始,然后是虚拟/抽象/密封,然后是覆盖/新/等。尽管其他人可能会这样做。然而,几乎无一例外地,访问修饰符将是第一个。

回答by Jeppe Stig Nielsen

In some cases there are very many possibilities. For example with the below class Cwith base class B,

在某些情况下,有很多可能性。例如下面的类C与基类B

public class B
{
  public void X()
  {
  }
}
public class C : B
{
  protected internal new static readonly DateTime X;
}

the field of type DateTimein Chas no fewer than five distinct modifiers, so there are 5! == 5*4*3*2*1 == 120different ways to write the same field! It would be veryconfusing not to have protectedand internalnext to each other, but it is still legal.

type DateTimein字段C有不少于五个不同的修饰符,所以有5! == 5*4*3*2*1 == 120不同的方法来写同一个字段!这将是非常令人困惑不要有protectedinternal彼此相邻,但它仍然是合法的。

Not sure if everyone agrees on a convention for the order. For example I have seen some people put the newmodifier beforethe access level (protection level) modifier, although many people like to always have the protection level modifier first.

不确定是否每个人都同意订单的约定。比如我看到有人把new修改的访问级别(保护级)改性剂,虽然很多人喜欢总是有保护级别修改第一。

回答by Wai Ha Lee

I had a look at Microsoft's Framework Design Guidelinesand couldn't find any references to what order modifiers should be put on members. Likewise, a look at the C# 5.0 language specificationproved fruitless. There were two other avenues to follow, though: EditorConfig filesand ReSharper.

我查看了 Microsoft 的框架设计指南,但找不到任何关于应该在成员上放置什么顺序修饰符的参考。同样,查看C# 5.0 语言规范也没有结果。不过,还有其他两种途径可以遵循:EditorConfig 文件ReSharper



.editorconfig

.editorconfig

The MSDN page, .NET coding convention settings for EditorConfigsays:

MSDN 页面,EditorConfig 的 .NET 编码约定设置说:

In Visual Studio 2017, you can define and maintain consistent code style in your codebase with the use of an EditorConfigfile.

Example EditorConfig file

To help you get started, here is an example .editorconfig file with the default options:

###############################
# C# Code Style Rules         #
###############################

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

在 Visual Studio 2017 中,您可以使用EditorConfig文件在代码库中定义和维护一致的代码样式。

示例 EditorConfig 文件

为了帮助您入门,这里有一个带有默认选项的示例 .editorconfig 文件:

###############################
# C# Code Style Rules         #
###############################

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

In other words: the default order for modifiers, following the default editorconfig settings is:

换句话说:修饰符的默认顺序,遵循默认的 editorconfig 设置是:

{ public / private / protected / internal / protected internal / private protected } // access modifiers
static
extern
new
{ virtual / abstract / override / sealed override } // inheritance modifiers
readonly
unsafe
volatile
async


ReSharper

锐化

ReSharper, however, is more forthcoming. The defaults for ReSharper 2018.11, with access modifiers (which are exclusive) and inheritance modifiers (which are exclusive), grouped together is:

然而,ReSharper更受欢迎。ReSharper 2018.1 1的默认值,带有访问修饰符(独占)和继承修饰符(独占),组合在一起是:

{ public / protected / internal / private / protected internal / private protected } // access modifiers
new
{ abstract / virtual / override / sealed override } // inheritance modifiers
static
readonly
extern
unsafe
volatile
async

This is stored in the {solution}.dotsettingsfile under the

这存储在{solution}.dotsettings文件下

"/Default/CodeStyle/CodeFormatting/CSharpFormat/MODIFIERS_ORDER/@EntryValue"

node - the ReSharper default2is:

节点 - ReSharper 默认值2是:

<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/MODIFIERS_ORDER/@EntryValue">
    public protected internal private new abstract virtual sealed override static readonly extern unsafe volatile async
</s:String>

1ReSharper 2018.1says that it has "Full understanding of C# 7.2" and explicitly mentions the private protectedaccess modifier.

1 ReSharper 2018.1表示它“完全理解 C# 7.2”并明确提到private protected访问修饰符。

2ReSharper only saves settings which differ from the default, so in general this node, as it is, will not be seen in the dotsettingsfile.

2ReSharper 仅保存与默认设置不同的设置,因此通常在dotsettings文件中不会看到该节点。



new staticvs static new

new static对比 static new

The MSDN page for Compiler Warning CS0108gives the example of a public field ion a base class being hidden by a public static field ion a derived class: their suggestion is to change staticto static new:

编译器警告 CS0108的 MSDN 页面给出i了基类上的公共字段被i派生类上的公共静态字段隐藏的示例:他们的建议是更改staticstatic new

public class clx
{
    public int i = 1;
}

public class cly : clx
{
    public static int i = 2; // CS0108, use the new keyword
    // Use the following line instead:
    // public static new int i = 2;
}
public class clx
{
    public int i = 1;
}

public class cly : clx
{
    public static int i = 2; // CS0108, use the new keyword
    // Use the following line instead:
    // public static new int i = 2;
}

Likewise, the IntelliSense in Visual Studio 2015 also suggests changing staticto static new

同样,Visual Studio 2015 中的 IntelliSense 也建议更改staticstatic new

CS0108 Visual Studio recommended change

CS0108 Visual Studio 建议更改

which is the same if the field iin the base class is also static.

如果i基类中的字段也是static.

That said, a cursory search on GitHub found that some projects override this default to put staticbefore, not afternew, the inheritance modifiers and sealed, e.g. the ReSharper settings for StyleCop GitHub project:

也就是说,在 GitHub 上粗略搜索发现,一些项目覆盖了这个默认设置,将继承修饰符放在static之前,而不是之后,例如 StyleCop GitHub 项目的 ReSharper 设置newsealed

<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/MODIFIERS_ORDER/@EntryValue">
    public protected internal private static new abstract virtual override sealed readonly extern unsafe volatile async
</s:String>

however since staticcannot be used in conjunction with the inheritance modifiers or sealed, this is just a distinction between new static(the default, and suggested by the default editorconfig file) and static new(suggested by ReSharper).

然而,由于static不能与继承修饰符 or 结合使用sealed,这只是new static(默认,由默认 editorconfig 文件static new建议)和(由 ReSharper 建议)之间的区别。

Personally I prefer the latter, but Google searches in referencesource.microsoft.comfor new staticvs static newin 2015 and 2018 gave:

我个人倾向于后者,但在谷歌搜索referencesource.microsoft.comnew staticVS static new,2015年和2018年给:

             (in 2015)  (in 2018)
new static   203        427
static new   10         990

which implies that the preference at Microsoft is static new.

这意味着微软的偏好是static new.