代码之家  ›  专栏  ›  技术社区  ›  tsilb

在C#中对方法修饰符排序是否有最佳实践?

  •  0
  • tsilb  · 技术社区  · 6 年前

    public , private protected , virtual , abstract , override new , static , internal , sealed

    0 回复  |  直到 10 年前
        1
  •  19
  •   plr108 JaredPar    7 年前

    StyleCop 可用作 Visual Studio extension NuGet package 并且可以根据Microsoft中某些团队使用的规则验证源代码。StyleCop希望访问修饰符优先。

        2
  •  16
  •   Wai Ha Lee captain-yossarian from Ukraine    7 年前

    我看过微软的 Framework Design Guidelines 找不到任何引用,无法引用应在成员上放置的顺序修饰符。同样,看看 C# 5.0 language specification 结果证明没有结果。不过,还有两条路要走: EditorConfig files ReSharper .


    .编辑配置

    MSDN页面, .NET coding convention settings for EditorConfig

    在Visual Studio 2017中,可以使用 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 不过,这一点更为直接。ReSharper 2018.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"
    

    节点-ReSharper默认值 是:

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

    ReSharper 2018.1 说它有 充分理解C#7.2 “并明确提到 private protected

    2个 ReSharper只保存与默认值不同的设置,因此通常在 dotsettings


    new static static new

    的MSDN页 Compiler Warning CS0108 i 基类被公共静态字段隐藏 static 静态新建

    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也建议改变 静态新建

    CS0108 Visual Studio recommended change

    静止的

    也就是说,对GitHub的粗略搜索发现,一些项目重写了这个默认设置 静止的 ,不是 new ,继承修饰符和 sealed ,例如。 the ReSharper settings for StyleCop GitHub project

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

    但是自从 不能与继承修饰符或 密封的 ,这只是 新静态 静态新建 (由雷斯哈珀建议)。

    我个人更喜欢后者,但谷歌搜索 referencesource.microsoft.com new static static new 2015年和2018年:

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

    静态新建

        3
  •  3
  •   Chris Charabaruk    17 年前

    我通常先从access修饰符开始,然后是virtual/abstract/sealed,然后是override/new/etc,尽管其他人可能会做得不同。然而,访问修饰符几乎总是第一个。

        4
  •  1
  •   Wai Ha Lee captain-yossarian from Ukraine    10 年前

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

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

    DateTime 在里面 C类 至少有五个不同的修饰符,所以 5! == 5*4*3*2*1 == 120 用不同的方法写同一个字段!可能是 非常 令人困惑的是 protected internal 彼此相邻,但仍然合法。

    不确定是否每个人都同意订单的约定。例如我见过一些人把 new 之前 访问级别(保护级别)修饰符,尽管许多人喜欢总是先有保护级别修饰符。

    推荐文章