如果我要使用多个,我应该使用修饰符关键字,例如:
public,private,protected,virtual,abstract,override,new,static,internal,sealed,,我忘了其他任何.
Mar*_*ath 19
如果您下载Microsoft StyleCop Visual Studio插件,它可以根据Microsoft使用的某些团队的规则验证您的源代码.它首先喜欢访问修饰符.
编辑:微软本身并不完全一致; 不同的团队使用不同的风格 例如.StyleCop建议在命名空间中使用指令; 但Roslyn源代码中没有遵循这一点.
Wai*_*Lee 15
我查看了Microsoft的框架设计指南,但找不到任何关于应该在成员上添加订单修饰符的引用.同样,看看C#5.0语言规范也没有结果.不过还有另外两个途径:EditorConfig文件和ReSharper.
MSDN页面,EditorConfig的.NET编码约定设置说:
在Visual Studio 2017中,您可以使用EditorConfig文件在代码库中定义和维护一致的代码样式.
示例EditorConfig文件
为了帮助您入门,下面是一个示例.editorconfig文件,其中包含以下默认选项:
Run Code Online (Sandbox Code Playgroud)############################### # 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更加即将到来.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
这存储在{solution}.dotsettings文件下
"/Default/CodeStyle/CodeFormatting/CSharpFormat/MODIFIERS_ORDER/@EntryValue"
node - 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>
1  ReSharper 2018.1表示它" 完全理解C#7.2 "并明确提到了private protected访问修饰符.
2 ReSharper仅保存与默认设置不同的设置,因此通常不会在dotsettings文件中看到此节点.
new static VS static new编译器警告CS0108的MSDN页面给出i了基类上的公共字段的示例,该公共字段被i派生类上的公共静态字段隐藏:他们的建议是更改static为static new:
Run Code Online (Sandbox Code Playgroud)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为static new
如果i基类中的字段也是相同的static.
也就是说,粗略搜索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>
但是因为static不能与继承修饰符一起使用,或者sealed这只是new static(默认的,由默认的editorconfig文件static new建议)和(由ReSharper建议)之间的区别.
我个人更喜欢后者,但谷歌在2015年和2018年的referencesource.microsoft.com上搜索了new staticvs static new:
             (in 2015)  (in 2018)
new static   203        427
static new   10         990
这意味着微软的偏好是static new.
| 归档时间: | 
 | 
| 查看次数: | 9859 次 | 
| 最近记录: |