C#代码规范——成员分段

来源:互联网 发布:video.js怎么播放flv 编辑:程序博客网 时间:2024/04/30 11:50

这里的基本整理一般原则是:

1. 尽可能少分段

2. 关于类的尽可能靠前(例如static),关于实际对象的尽可能靠后

3. 早生成的尽可能靠前

4. 公有的,接口的尽可能靠前

5. 抽象的,通用的,基础性的,被依赖靠前;继承过来的尽量靠前

6. 相对需要引起注意的尽量靠前

7. 其他一些以往经验考虑

class Sample : BaseClass, IIntf1{#region Enumerations    enum EnumType    {        Const1,        // ...    }#endregion#region delegates         public void SomeEventDelegate(EventArgs e);    // ...    #endregion#region Nested types    public class PubNestedClass    {        // ...    }    private class PrivNestedClass    {     }    #endregion#region Fields    public static int InstanceCount = 0;    public int IntValue = 30;    private static int _count = 0;    private int _privField;    private const _constantField;    private static _staticField;    private SomeEventDelegate _eventHandlers;  #endregion#region Constructors    static Sample()    // static constructor first if any    {    }         public Sample(int a, int b)    // constructor with the most comprehensive parameters    {       // ...    }    public Sample(int a) : this(a,0)    {       // ...    }        ~Sample()    {    }#endregion#region Properties#region IIntf1 members#region IIntf1.1 members // an interface that IIntf1 implements if any    // ...#endregion    public int Id { get; private set; } #endregion#region BaseClass members    public new string BaseClassPropertyToOverwrite { ... }    public override string BaseClassProperty { get {...} set {...} }#endregion    public static int Count { get; private set; }    public string Name { get; private set; }    private int PrivProperty { get; set; } #endregion#region Methods    // ... #endregion#region Events     public event SomeEventDelegate { add { _eventHandlers += value; } remove { ... } }#endregion}// Order of execution// 1. initialisation of fields with initialisers// 2. static constructor// 3. static member or object constructor// 4. member invocation// 5. destructor// * nested types are instantiated when it's touched during any of the above steps and as per the same rule


 

原创粉丝点击