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

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

问题描述

有没有一个标准的方式来布局一个C#文件?像在,字段,然后属性,然后构造函数等?



这是我通常做的,但我想知道是否有一个标准的方法?


  1. 嵌套类或枚举

  2. 字段

  3. 属性

  4. 活动

  5. 构造函数

  6. 公共方法


人们将他们的字段分组在一起,还是将他们放在属性中?或者人们不担心订单? Visual Studio似乎使它这么难做。



编辑:移动其他部分关于ReSharper这里:

我倾向于使用 Microsoft StyleCop ,它根据规则 SA1201


原因 C#代码
文件中的元素与
的代码中的其他元素无关。



规则描述当文件中的代码元素
不符合标准
排序方案时,会发生违反此
规则的情况。



为符合此规则,
元素的文件根级别或
命名空间中的元素必须按照顺序放置在
中:




  • 外部别名指令

  • 使用指令

  • 命名空间

  • 代表

  • 枚举

  • 接口

  • / li>



在类,结构或接口中,
元素必须位于
中的顺序:




  • 字段

  • 构造函数

  • Finalizers(Destructors)

  • 代表

  • 活动

  • 枚举

  • 接口

  • 属性

  • 索引器

  • 方法

  • 结构




符合标准订购
基于元素类型的方案可以
提高文件的可读性和
可维护性,
鼓励代码重用。



实现接口时,有时候需要将
的所有
成员分组到另一个
旁边。这有时需要违反这条规则的
,如果接口
包含不同类型的元素。
这个问题可以通过
使用部分类来解决。



  1. 添加另一个具有相同名称的部分类。可以将
    放在同一个文件中,就在
    原始类下面,或者在第二个
    文件中。


  2. 将界面继承和界面的所有成员
    实现移到
    类的第二部分。




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天全站免登陆