我看了微软的框架设计指南 https://msdn.microsoft.com/en-us/library/ms229042%28v=vs.100%29并且找不到任何关于应该对成员添加什么顺序修饰符的参考。同样,看一下C# 5.0 语言规范 https://www.microsoft.com/en-gb/download/details.aspx?id=7029事实证明毫无结果。还有另外两个途径可以遵循:编辑器配置文件 https://learn.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017 and 锐锐 https://www.jetbrains.com/resharper.
.editorconfig
MSDN 页面,EditorConfig 的 .NET 编码约定设置 https://learn.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017 says:
在 Visual Studio 2017 中,您可以使用编辑器配置 https://learn.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017 file.
编辑器配置文件示例
为了帮助您入门,下面是一个带有默认选项的 .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
换句话说:遵循默认 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 https://www.jetbrains.com/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:
{ public / protected / internal / private / protected internal / private protected } // access modifiers
new
{ abstract / virtual / override / sealed override } // inheritance modifiers
static
readonly
extern
unsafe
volatile
async
这存储在{solution}.dotsettings
文件下的
"/Default/CodeStyle/CodeFormatting/CSharpFormat/MODIFIERS_ORDER/@EntryValue"
node - the ReSharper default2 is:
<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>
1 ReSharper 2018.1 https://www.jetbrains.com/resharper/whatsnew/#v2018-1 says that it has "Full understanding of C# 7.2" and explicitly mentions the private protected
access modifier.
2 ReSharper only saves settings which differ from the default, so in general this node, as it is, will not be seen in the dotsettings
file.
new static
vs static new
MSDN 页面编译器警告 CS0108 https://msdn.microsoft.com/en-us/library/3s8070fc.aspx给出公共领域的例子i
基类被公共静态字段隐藏i
在派生类上:他们的建议是改变static
to static 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;
}
同样,Visual Studio 2015 中的 IntelliSense 也建议更改static
to static new
如果字段是相同的i
在基类中也是static
.
也就是说,在 GitHub 上粗略搜索发现某些项目会覆盖此默认值以放置static
before, not after new
,继承修饰符和sealed
, e.g.
StyleCop GitHub 项目的 ReSharper 设置 https://github.com/StyleCop/StyleCop/blob/master/Project/Src/AddIns/ReSharper/StyleCop.dotSettings#L75:
<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>
然而自从static
不能与继承修饰符一起使用或sealed
,这只是之间的区别new static
(默认值,并由默认编辑器配置文件建议)和static new
(由 ReSharper 建议)。
我个人更喜欢后者,但谷歌搜索参考source.microsoft.com http://referencesource.microsoft.com/ for new static https://www.google.co.uk/?gws_rd=ssl#q=inurl:referencesource.microsoft.com%2F+%22new+static%22 vs static new https://www.google.co.uk/?gws_rd=ssl#q=inurl:referencesource.microsoft.com%2F+%22static+new%222015年和2018年给出:
(in 2015) (in 2018)
new static 203 427
static new 10 990
这意味着微软的偏好是static new
.