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

2023-12-19

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

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


我看了微软的框架设计指南 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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中修饰符的顺序有约定吗? 的相关文章

  • 与 MinGW 的静态和动态/共享链接

    我想从一个简单的链接用法开始来解释我的问题 假设有一个图书馆z它可以编译为共享库 libz dll D libs z shared libz dll 或静态库 libz a D libs z static libz a 让我想要链接它 然后
  • 来自 double 的 static_cast 可以优化分配给 double 吗?

    我偶然发现了一个我认为不必要的功能 并且通常让我感到害怕 float coerceToFloat double x volatile float y static cast
  • 如何使用T4从一个模板同时生成两个文件?

    我遇到的情况是 我需要生成两个 CSharp 代码文件 它们的代码几乎相同 但方法的输入和输出类型的命名空间不同 事实上 每个文件都针对特定国家 地区 并且类型来自特定国家 地区的 WSDL 我正在围绕服务编写一些包装器 逻辑完全相同 但从
  • C++中类成员函数相互调用有什么好处?

    我是 C 新手 我发现下面的编程风格对我来说很有趣 我在这里写了一个简化版本 include
  • Visual Studio 2013 调试器显示 std::string 的奇怪值

    我有一个大型的 cmake 生成的解决方案 其中包含许多项目 由于某种原因 我无法查看字符串的内容 因为根据调试器 Bx Buf含有一些垃圾 text c str 正确返回 Hello 该问题不仅仅发生在本地字符串上 返回的函数std st
  • 获取列表框中视图中的项目

    我有一个 ListBox 其属性 VirtualizingStackPanel VirtualizationMode 设置为 回收 我正在绑定一个自定义集合 实现IList and IList
  • 如何在 C# 中以编程方式将行添加到 DataGrid?

    正如标题所述 我正在尝试使用 C 以编程方式将行添加到 DataGrid 但我似乎无法使其工作 这是我到目前为止所拥有的 I have a DataGrid declared as dg in the XAML foreach string
  • X 轴和 Z 轴上的 Quaternion.Slerp,无 Y 轴

    I am trying to rotate the Player about X Y and Z axis The Y axis should not move from last angle Example if I rotate 45
  • 如何在 C++ 中正确使用 cin.fail()

    我正在编写一个程序 从用户那里获取整数输入cin gt gt iUserSel 如果用户输入一个字母 程序就会进入无限循环 我试图用下面的代码来阻止这种情况 但程序进入无限循环并打印出 错误 输入 我该如何修复我的程序 cin gt gt
  • 当我尝试传递临时地址作为参数时,它是一个 UB 吗?

    对于以下 C 代码 include
  • 为什么连续抛出 2 个异常不会生成无法访问的代码警告?

    为什么以下代码行不会创建编译器警告 void Main throw new Exception throw new Exception 据我所知 编译器应该通知您无法到达第二个抛出异常 这显然是一个编译器错误 它是在 C 3 0 中引入的
  • C# 可以为控制台应用程序部分类“程序”类吗?

    我想知道是否可以将为任何控制台应用程序创建的默认 程序 类更改为部分类 我想这样做是因为我想要更好的组织 而不是将所有方法都放在按区域分类的 1 个文件中 对我来说 将某些方法类别放在单独的文件中会更有意义 我对分部类的理解是 它是多个文件
  • main.cpp 是必需的吗?

    我试图编译一个程序cmake 我最终删除了我的main cpp文件 我刚刚将其复合到另一个包含我的项目名称的文件中 即 我刚刚将主函数剪切并粘贴到该文件中 问题是我有一个main cpp未发现错误 不确定是否在C 一个名为main cpp是
  • MPI - 发送和接收列

    我需要从一个进程发送矩阵列并从另一个进程接收它 我尝试运行以下程序 但得到了一个奇怪的结果 至少我这么认为 仅复制矩阵的第一个元素 某些矩阵元素会发生意外变化 include
  • 在 Visual Studio 2012 Express 中设置 C++ 调试环境

    我需要调试的应用程序需要设置环境变量 这在 Visual Studio 2012 中似乎非常复杂 我想做类似的事情 set path c foo c bar c windows c program files application set
  • g++ / gcc 是否支持 C++20 新的atomic_flag 功能?

    根据参考参数 https en cppreference com w cpp atomic atomic flag c 20 有丰富的 对我来说有用的 支持atomic flag运营 然而 目前尚不清楚 gcc 是否支持这些功能 它们在任何
  • 使用未命名命名空间而不是静态命名空间

    我可以假设在未命名命名空间中声明的对象相当于static namespace int x 1 static int x 2 FWIK 在这两种情况下 x将具有静态存储期限和内部链接 声明为的对象的所有规则也是如此static适用于未命名名称
  • 为什么我可以在另一个函数中定义一个函数?

    请参阅下面的代码 我在另一个函数中定义了一个函数 void test1 void void test2 void printf test2 n printf test1 n int main void test1 return 0 这个用法
  • c++ - <未解析的重载函数类型>

    在我的班级里叫Mat 我想要一个将另一个函数作为参数的函数 现在我有下面 4 个函数 但是在调用 print 时出现错误 第二行给了我一个错误 但我不明白为什么 因为第一行有效 唯一的区别是功能f不是班级成员Mat but f2是 失败的是
  • 如何将模型绑定到动态创建的类 nancyfx

    首先感谢任何愿意查看我的问题的人 我对 Nancyfx 还很陌生 在尝试将 JSON 有效负载绑定到动态创建的类时遇到问题 我按照这篇文章中的代码动态创建了该类 在C 中动态创建一个类 https stackoverflow com que

随机推荐