最新文檔地址https://github.com/hiramtan/CSharpNamingGuidelines_Chinesehtml
/******************************************************************* * Description:This is a example class * Version: 1.0.0 * Date: 20180218 * Author: Hiram * Email: hiramtan@live.com * Copyright @ www.wikipedia.org *******************************************************************/ using System; /// <summary> /// 命名空間(Pascal) /// </summary> namespace Namespace { /// <summary> /// 類(Pascal) /// </summary> public class Class { /// <summary> /// 屬性(Pascal) /// </summary> public int Property { get; set; } /// <summary> /// 委託(Pascal) /// EventHandler後綴 /// </summary> public delegate void EvnetHandler(); /// <summary> /// 事件(Pascal):On前綴 /// </summary> public event EvnetHandler OnEvent; /// <summary> /// 公有字段(Pascal) /// </summary> public int Field1; /// <summary> /// 受保護字段(Pascal) /// </summary> protected int Field2; /// <summary> /// 私有字段(Camel) /// </summary> private int field3; /// <summary> /// 方法(Pascal) /// </summary> /// <param name="args">參數(Camel)</param> public void Method(int args) { //局部變量(Camel) int variable = 10; } } /// <summary> /// 接口(Pascal) /// </summary> public interface IInterface { /// <summary> /// 屬性接口(Pascal) /// </summary> int Property { get; set; } /// <summary> /// 方法接口(Pascal) /// </summary> /// <param name="args"></param> void Method(int args); } /// <summary> /// 枚舉(Pascal) /// </summary> enum Enum { Enum1,//枚舉值(Pascal) Enum2,//枚舉值(Pascal) } }
全部的命名都是如下面兩種方式進行命名:git
類型 | 命名方式 | 示例 |
---|---|---|
Namespace | Pascal | namespace System.Security { ... } |
Type | Pascal | public class StreamReader { ... } |
Interface | Pascal | public interface IEnumerable { ... } |
Method | Pascal | public virtual string ToString(); |
Property | Pascal | public int Length { get; } |
Delegate | Pascal | public delegate void EvnetHandler(); |
Event | Pascal | public event EventHandler Exited; |
Public Field | Pascal | public int Min = 0; |
Protected Field | Pascal | public int Min = 0; |
private Field | Camel | public int min = 0; |
Enum | Pascal | public enum FileMode |
Parameter | Camel | public static int ToInt32(string value) |
Local Variable | Camel | void Method(){int number = 10;} |
註釋github
文件註釋: 文件註釋以以下方式進行,在擴展註釋時(回車換行時)會自動添加註釋行.api
/******************************************************************* * Description:This is a example class * Version: 1.0.0 * Date: 20180218 * Author: Hiram * Email: hiramtan@live.com * Copyright @ www.wikipedia.org *******************************************************************/命名空間 類型 接口 方法名 屬性 事件 字段 枚舉:以"///"的方式註釋,編輯器會自動完善註釋,而且能夠用生成工具直接生成代碼註釋文檔.編輯器
/// <summary> /// 方法(Pascal) /// </summary> /// <param name="args">參數(Camel)</param> public void Method(int args) { //局部變量(Camel) int variable = 10; }其餘註釋:以"//"的方式註釋在上一行或行尾添加註釋(好比局部變量,邏輯行) 段落註釋:以以下方式進行ide
/* public void Method() { } */待辦及高亮:在註釋中添加"ToDo"高亮顯示註釋工具
命名空間ui
類型編碼
泛型默認T,U,V,W...spa
接口
接口定義以"I"開頭,好比
IInterface
,IPlayer
方法
屬性
屬性都以Pascal方式命名
委託
微軟官方建議添加如下兩種後綴
- EventHandler
- Callback 微軟官方不建議添加如下後綴
Delegate
事件
- 微軟官方建議:事件參數添加後綴"EventArgs"
- 習慣命名:事件以On爲前綴(好比OnClick)
字段
微軟官方只規定了public/protected以Pascal方式命名,對internal,private類型的字段沒有說明,所以各類第三方規範和插件中對私有字段規範也不一致. 針對官方的示例代碼,書寫習慣,智能提示,代碼補全和約定俗成的C#規範,建議private採用Camel方式命名,非Private字段採用Pascal方式命名.
- public/protected/internal以Pascal方式命名
- private以Camel方式命名
public int Field1; public static int Field2; public readonly int Field3; public const int Field4 = 4; internal int Field5; internal static int Field6; internal readonly int Field7; internal const int Field8 = 8; private int field9; private static int field10; private readonly int field11; private const int field8 = 12;
以m_爲前綴的方式不建議(C++命名方式)以_爲前綴的方式不建議(ReSharper命名方式)以類型爲前綴的方式不建議(好比bool類型的字段以b爲前綴,枚舉以e爲前綴)以類型爲後綴的方式不建議(好比單位列表unitList,直接取名爲units)
枚舉
參數
局部變量
局部變量能夠使用var自動聲明類型