什么是布置一个C#类的最佳方式? [英] What's the best way to layout a C# class?

查看:99
本文介绍了什么是布置一个C#类的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有铺设了一个C#文件的标准方法?正如,字段,然后属性,然后构造等?



下面就是我通常做,但我不知道是否有一个标准的方式?




  1. 嵌套类或枚举

  2. 字段

  3. 属性

  4. 活动

  5. 构造

  6. 的公共方法

  7. 私有方法



难道一群人各自领域在一起,或者他们把他们的属性?或者说人们不担心订单? Visual Studio中似乎使它所以很难做到。



修改:移动约ReSharper的另一部分此处的让ReSharper的尊重您的喜好代码顺序。


解决方案

我倾向于使用微软了StyleCop ,其中有根据一组为了统治的 SA1201




原因 C#代码
文件中的元素是无序相对于
在代码中的其他元素。



规则说明
规则的违反,当一个文件中的代码元素
不按标准
排序方案出现。



要遵守此规则,在
文件根级别或
命名空间内的元素必须按照以下顺序定位在




  • 外部别名指令

  • 使用指令

  • 命名空间

  • 代表

  • 枚举

  • 接口

  • 结构体




在类,结构或接口,
元素必须是定位于
顺序如下:




  • 字段

  • 构造

  • 终结(析构函数)

  • 代表

  • 活动

  • 枚举

  • 接口

  • 属性

  • 索引器

  • 方法

  • 结构体




与标准排序
符合基于元素类型方案可以
增加文件和
的可读性和
可维护性鼓励代码重用。



当实现一个接口,它是
有时需要组的接口都
成员挨着一家
另一回事。这有时需要
违反这个规则,如果接口
包含不同类型的元素。
这个问题可以通过
使用局部类来解决。




  1. 添加部分属性类,如果类是不是已经
    部分。


  2. 添加第二个局部类具有相同的名称。这是可能放置
    。这种在同一个文件,就在
    原始类的下方,或第二
    文件内


  3. 移动接口继承和接口
    实施了
    类的第二部分中的所有成员。




Is there a standard way of laying out a C# file? As in, Fields, then Properties, then Constructors, etc?

Here's what I normally do, but I'm wondering if there's a standard way?

  1. Nested Classes or Enums
  2. Fields
  3. Properties
  4. Events
  5. Constructors
  6. Public Methods
  7. Private Methods

Do people group their fields together, or do they put them with the properties? Or do people not worry about an order? Visual Studio seems to make it so hard to do.

Edit: Moved other part about ReSharper here: Make Resharper respect your preference for code order.

解决方案

I tend to use Microsoft StyleCop, which has a set order according to rule SA1201:

Cause An element within a C# code file is out of order in relation to the other elements in the code.

Rule Description A violation of this rule occurs when the code elements within a file do not follow a standard ordering scheme.

To comply with this rule, elements at the file root level or within a namespace must be positioned in the following order:

  • Extern Alias Directives
  • Using Directives
  • Namespaces
  • Delegates
  • Enums
  • Interfaces
  • Structs
  • Classes

Within a class, struct, or interface, elements must be positioned in the following order:

  • Fields
  • Constructors
  • Finalizers (Destructors)
  • Delegates
  • Events
  • Enums
  • Interfaces
  • Properties
  • Indexers
  • Methods
  • Structs
  • Classes

Complying with a standard ordering scheme based on element type can increase the readability and maintainability of the file and encourage code reuse.

When implementing an interface, it is sometimes desirable to group all members of the interface next to one another. This will sometimes require violating this rule, if the interface contains elements of different types. This problem can be solved through the use of partial classes.

  1. Add the partial attribute to the class, if the class is not already partial.

  2. Add a second partial class with the same name. It is possible to place this in the same file, just below the original class, or within a second file.

  3. Move the interface inheritance and all members of the interface implementation to the second part of the class.

这篇关于什么是布置一个C#类的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆