您的位置:首页 > 编程语言 > C#

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

2013-04-23 09:07 260 查看
这里的基本整理一般原则是:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: