C#编码标准和命名规范

以下是C#编码标准,命名规范,还有一些最佳实践。
在你的项目里使用这些规范和(或者)调整这些适应你的需求。
类型名称和方法名称使用PascalCasing书写

public class ClientActivity
{
    public void ClearStatistics()
    {
        //...
    }
    public void CalculateStatistics()
    {
        //...
    }
}

方法参数和局部变量使用camelCasing书写

public class UserLog
{
    public void Add(LogEvent logEvent)
    {
        int itemCount = logEvent.Items.Count;
        // ...
    }
}

禁止使用匈牙利标记法或者在标识符前面增加类型标识

// 正确
int counter;
string name;

// 避免
int iCounter;
string strName;

禁止使用大写标识常量或者只读型变量

// 正确
public static const string ShippingType = "DropShip";

// 避免
public static const string SHIPPINGTYPE = "DropShip";

避免使用缩写。例外情况:常用名称的缩写,如 Id, Xml, Ftp, Uri

//正确
UserGroup userGroup;
Assignment employeeAssignment;

// 避免
UserGroup usrGrp;
Assignment empAssignment;

// 例外
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;

3个或者3个以上的字母缩写使用PascalCasing(2个字母使用大写)

HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;

标识符不要使用下划线,例外:私有静态变量可以使用下划线

// 正确
public DateTime clientAppointment;
public TimeSpan timeLeft;

// 避免
public DateTime client_Appointment;
public TimeSpan time_Left;

// 例外
private DateTime _registrationDate;

使用预定义的类型名称替代系统类型名称,例如Int16, Single, UInt64等

// 正确
string firstName;
int lastIndex;
bool isSaved;

// 避免
String firstName;
Int32 lastIndex;
Boolean isSaved;

局部变量声明使用隐式类型var。例外:主要类型((int, string, double等)使用预定义的类型。

var stream = File.Create(path);
var customers = new Dictionary();

// 例外
int index = 100;
string timeSheet;
bool isCompleted;

类使用名词或者名词短语命名。

public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}

接口前缀使用字母I,接口名称使用名词(名词短语)或者连接词。

public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}

源文件命名参照他们的主类,例外:部分类的文件名反映他们的来源或目的,例如: designer, generated等。

// Located in Task.cs
public partial class Task
{
    //...
}

// Located in Task.generated.cs
public partial class Task
{
    //...
}

组织命名空间,命名空间要有清晰明确的结构

// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group

大括号要竖直对齐

// 正确
class Program
{
    static void Main(string[] args)
    {
    }
}

类的顶部声明成员变量,静态类型的变量在最前面

// 正确
public class Account
{
    public static string BankName;
    public static decimal Reserves;

    public string Number {get; set;}
    public DateTime DateOpened {get; set;}
    public DateTime DateClosed {get; set;}
    public decimal Balance {get; set;}

    // Constructor
    public Account()
    {
        // ...
    }
}

枚举类型使用单数名称。例外:位字段枚举。

// 正确
public enum Color
{
    Red,
    Green,
    Blue,
    Yellow,
    Magenta,
    Cyan
}

// 例外
[Flags]
public enum Dockings
{
    None = 0,
    Top = 1, 
    Right = 2, 
    Bottom = 4,
    Left = 8
}

不要显示指定枚举的数据类型或者枚举的值(位字段枚举除外)

// 避免
public enum Direction : long
{
    North = 1,
    East = 2,
    South = 3,
    West = 4
}

// 正确
public enum Direction
{
    North,
    East,
    South,
    West
}

不要在枚举名词加后缀Enum

//避免
public enum CoinEnum
{
    Penny,
    Nickel,
    Dime,
    Quarter,
    Dollar
}

// 正确
public enum Coin
{
    Penny,
    Nickel,
    Dime,
    Quarter,
    Dollar
}

原文地址:
http://www.dofactory.com/reference/csharp-coding-standards

发布了50 篇原创文章 · 获赞 6 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章