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

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

问题描述

有关于课程结构方面的项目顺序的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)



  • 字段

  • 构造函数

  • Finalizer(析构函数)


  • 活动

  • 枚举

  • 接口

  • li>
  • 索引器

  • 方法

  • 结构


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


  • 公开

  • 内部

  • 受保护的内部

  • 受保护
  • li> private
  • public
  • internal
  • protected internal
  • protected
  • private

在每个访问组中,按静态顺序,然后是非静态顺序:(SA1204)

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


  • static

  • 非静态

在每个静态/非静态字段组中,按唯读顺序,然后以非唯读顺序排序:(SA1214和SA1215)

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


  • readonly

  • 非唯读

已展开的清单长度为130行,所以我不会在这里展开。展开的方法部分是:

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


  • public static methods

  • 公共方法

  • 内部静态方法

  • 内部方法


  • 保护的静态方法

  • 受保护的方法
  • li>私有方法
  • 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

文档注意到如果规定的顺序不合适---说正在实现多个接口,并将接口方法和属性分组在一起 - 然后使用部分类将相关方法和属性组合在一起。

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