秩序类项目:字段,属性,构造函数,方法 [英] Order of items in classes: Fields, Properties, Constructors, Methods

查看:170
本文介绍了秩序类项目:字段,属性,构造函数,方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有项目的顺序在课堂结构方面一个C#官方指导?

Is there a C# official guideline for the order of items in terms of class structure?

是否去:

  • 在公共领域
  • 在私人领域
  • 属性
  • 构造
  • 方法
  • Public Fields
  • Private Fields
  • Properties
  • Constructors
  • Methods
    ?

我很好奇,如果有一个关于项目的顺序一个硬性规定?我有点所有的地方。我想坚持一个特定的标准,所以我可以到处去做。

I'm curious if there is a hard and fast rule about the order of items? I'm kind of all over the place. I want to stick with a particular standard so I can do it everywhere.

真正的问题是我比较复杂的特性最终看起来很像方法,他们觉得格格不入的构造函数之前的顶部。

The real problem is my more complex properties end up looking a lot like methods and they feel out of place at the top before the constructor.

任何提示/建议?

推荐答案

按照了StyleCop规则文档的顺序如下所示。

在一个类,结构或接口:(SA1201和SA1203)

Within a class, struct or interface: (SA1201 and SA1203)

  • 恒田
  • 字段
  • 构造
  • 终结(析构函数)
  • 代表
  • 活动
  • 枚举
  • 接口
  • 属性
  • 索引器
  • 方法
  • 结构体
  • Constant Fields
  • Fields
  • Constructors
  • Finalizers (Destructors)
  • Delegates
  • Events
  • Enums
  • Interfaces
  • Properties
  • Indexers
  • Methods
  • Structs
  • Classes

在每个组的顺序按访问:(SA1202)

Within each of these groups order by access: (SA1202)

  • 公开
  • 内部
  • 在受保护的内部
  • 保护
  • 私人

在每一个访问组,以通过静态的,那么非静态的:(SA1204)

Within each of the access groups, order by static, then non-static: (SA1204)

  • 静态
  • 在非静态

在每一个静止/非静态组字段,为了通过只读的,那么非只读:(SA1214和SA1215)

Within each of the static/non-static groups of fields, order by readonly, then non-readonly : (SA1214 and SA1215)

  • 在只读
  • 非只读

这是展开的列表是130线长,所以我不会解开它在这里。该方法的一部分展开的:

An unrolled list is 130 lines long, so I won't unroll it here. The methods part unrolled is:

  • 在公共静态方法
  • public方法
  • 在内部静态方法
  • 内部方法
  • 在受保护的内部静态方法
  • 在受保护的内部方法
  • 在受保护的静态方法
  • 在受保护的方法
  • 私有静态方法
  • 私有方法
  • public static methods
  • public methods
  • internal static methods
  • internal methods
  • protected internal static methods
  • protected internal methods
  • protected static methods
  • protected methods
  • private static methods
  • private methods

文档注意到,如果prescribed顺序不适合---比如,多个接口正在执行,以及接口的方法和属性应该组合---然后使用局部类来组相关的方法和属性一起

The documentation notes that if the prescribed order isn't suitable --- say, multiple interfaces are being implemented, and the interface methods and properties should be grouped together --- then use a partial class to group the related methods and properties together.

这篇关于秩序类项目:字段,属性,构造函数,方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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