C#/.NET编码规范

一、             术语定义

 

Pascal 命名法

标识符的首字母和后面连接的每个单词首字母大写。例如:ProductName

 

Camel 命名法

标识符的首字母小写,而后面连接的每个单词首字母大写。例如:userFamilyAddress

 

 

二、             命名规范

 

(1)     文件

文件名遵从Pascal 命名法,无特殊情况,扩展名小写。例如:SqlHelper.cs

 

(2)     命名空间

1.       命名命名空间的一般性规则是使用公司名称,后跟项目名称和功能模块名称。

2.       命名空间的命名遵从Pascal 命名法。

例如:

IBM.AttendanceSystem      // IBM的考勤管理系统

IBM.AttendanceSystem.IDAL  // IBM的考勤管理系统的数据访问层接口模块

 

(3)    

1.       由名词构成,名词使用全称,如果是一种公认的约定名词可以缩写,如URL

2.       一般情况下,派生类的第二部分应为基类名称。

3.       遵从Pascal 命名法。

例如:

public class Order

public class CartItemInfo

public class PetShopProfileProvider : ProfileProvider

public class Category : TableDependency

 

(4)     接口

1.       由名词,或者描述行为的形容词构成。

2.       以大写字母I开头。

3.       类实现接口时,要使用相似的名称。

4.       遵从Pascal命名法。

例如:

public interface IInventory

public interface IPetShopProfileProvider

public interface IFormatalbe

public interface IComponent

public class Component : IComponent

 

(5)     函数

1.       使用动词或动宾结构。

2.       遵从Pascal命名法。

例如:

Invoke()

SetCartItems()

GetAccountInfo()

 

(6)     属性

1.       使用名词构成。

2.       遵从Pascal命名法。

例如:

private int _quantity = 1;

//属性

public int Quantity

{

    get

    {

        return _quantity;

    }

    set

    {

        _quantity = value;

    }

}

 

(7)     事件处理程序和事件

1.       对事件处理程序名称使用EventHandler后缀。

2.       两个参数名称为senderesender表示引发事件的对象,始终是object类型的。与事件相关的状态封装在名为e的事件参数中。

3.       EventArgs后缀命名事件参数。

4.       用动词命名事件。用ing形式表示事件前的概念,用过去式表示事件后。

5.       通常情况下,对于可以在派生类中重写的事件,应在类型上提供一个受保护的方法(称为OnXxx)。此方法只应具有事件参数e,因为发送方总是类型的实例。

6.       遵从Pascal命名法。

例如:

//具有适当名称和参数的事件处理程序

public delegate void MouseEventHandler(object sender, MouseEventArgs e);

//事件的命名

public event EventHandler Disposed

public event EventHandler DataBinding

 

(8)     常量

所有单词大写,多个单词间用下划线_隔开。

例如:

private const string PARM_PRODUCT_ID = "@ProductId";

private const string PARM_ITEM_ID = "@ItemId";

 

(9)     变量

1.       遵从Camel命令法。

2.       类的成员变量名称以下划线_开头。

3.       布尔类型的变量,变量名以is开头。

例如:

string userName

string applicationName

int _productId

bool isAuthenticated

bool isShoppingCart

 

10、控件:由缩写打头,后面的标识符遵从Pascal命名法。常用控件的缩写如下:

 

控件类型

命名示例

Button

btnAddbtnDeletebtnSave

CheckBox

chkRed

CheckBoxList

chklstFavoriteSports

DropDownList

dropCountries

Label

lblResults

ListBox

lstbxRegions

RadioButton

radbtnGender

TextBox

txtbxFirstName

Table

tblCountryCode

Form

formBilling

GroupBox

grpbxMain

ComboBox

cbobxMenu

DataList

dlstTitles

DataGrid

dgrdTitles

GridView

grdvwCategory

CompareValidator

valcmpPasswordError

CustomValidator

valcusAddressError

RangeValidator

valranYearError

RegularExpressValidator

valregexpIdentityCardError

RequiredFieldValidator

valreqfiePassport

ValidationSummary

valsumaNameError

Menu

menuServices

TreeView

trvwBookCategory

以下是HTML控件

Input (Button)

hbtnOk

Input (Reset)

hrbtnReset

Input (Submit)

hsbtnSubmit

Input (Text)

htxtName

Input (File)

hfileProfileImage

Input (Password)

hpwdPassword

Input (Checkbox)

hchkRed

Input (Radio)

hradGender

Input (Hidden)

hhidAuthor

Textarea

htxtareaIntroductions

Table

htblBookSales

Image

himgMrLi

Select

hselPostCode

 

11ADO.NET对象:由缩写打头,后面的标识符遵从Pascal命名法。示例如下:

 

ADO.NET对象

命名示例

Connection

connMaster

Command

cmdReturnProducts

Parameter

paraProductId

DataAdapter

dadpProducts

DataReader

drdrProducts

DataSet

dsetNorthWind

DataTable

dtblProduct

DataRow

drowStudent

DataColumn

dcolProductName

DataRelation

drelMasterDetail

DataView

dvwDepartment

 

 

三、             代码外观

 

(1)     列宽

代码列宽控制在110 字符左右。

 

(2)     换行

(3)     缩进

形成层级的相邻代码行之间要有4个空格的缩进。

 

(4)     空行

(5)     空格

1.       关键字和左括号之间

例如:while (true)

注意:方法名与左括号之间不需要空格。

 

2.       多个参数用逗号隔开,每个逗号后都应加一个空格。

3.       除了.之外,所有的二元操作符都应用空格与它们的操作数隔开。一元操作符、++--与操作 数间不需要空格。

例如:

a += c + d;

a = (a + b) / (c * d);

while (d++ = s++)

{

    n++;

}

PrintSize("size is " + size + "\n");

 

4.       语句中的表达式之间用空格隔开。

for (expr1; expr2; expr3)

 

(6)     括号

      1、左括号“( 不要紧靠关键字,中间用一个空格隔开。

      2、左括号“( 与方法名之间不要添加任何空格。

      3、没有必要的话不要在返回语句中使用()

    例如:

      if (condition)

      Array.Remove(1)

      return 1

 

(7)     花括号

     1、左花括号 { 放于关键字或方法名的下一行并与之对齐。

    例如:

      if (condition)

     {

     }

     public int Add(int x, int y)

     {

     }

     2、左花括号 { 要与相应的右花括号 }”对齐。

     3、通常情况下左花括号 {”单独成行,不与任何语句并列一行。

     4ifwhiledo 语句后一定要使用{},即使{}号中为空或只有一条语句。如

     if (somevalue == 1)

     {

         somevalue = 2;

     }

     5、右花括号 } 后建议加一个注释以便于方便的找到与之相应的 {。如

     while (1)

     {       

        if (valid)

         {

         } // if valid

         else

         {

         } // not valid

     } // end forever

 

 

四、             语句

 

(1)     一条语句独占一行

例如:int age;  //推荐

      int xCoordinate, yCoordinate; //不推荐

 

(2)     复合语句

1.       子语句要右缩进4个空格。

2.       左右花括号独占一行。

3.       只有一条子语句时,花括号不能省略。

例如:

while (sum < 100)

{

sum++;

}

 

(3)     条件语句:if, if-else, if-else if

if (condition)

{

statements;

}

 

if (condition)

{

statements;

}

else

{

statements;

}

 

if (condition)

{

statements;

}

else if (condition)

{

statements;

}

else

{

statements;

}

 

(4)     循环语句for, foreach

     for (initialization; condition; update)

     {

         statements;

     }

   

     for (initialization; condition; update);

       

     foreach (object obj in array)

     {   

         statements;

     }

   

(5)     循环语句while

     while (condition)

     {

         statements;

     }

 

     while (condition);

 

(6)     循环语句do-while

     do

     {

         statements;

     } while (condition);

 

(7)     选择语句switch-case

     switch (condition)

     {

         case 1:

             statements;

             break;

         case 2:

             statements;

             break;

         default:

             statements;

             break;

     }

 

(8)     try-catch语句

     try

     {

         statements;

     }

     catch (ExceptionClass e)

     {

         statements;

     }

     finally

     {

         statements;

     }

 

(9)     goto语句

     goto Label1:

         statements;

     Lable1:

         statements;

 

 

五、             注释

 

(1)     函数注释

    /// <summary>

    /// Get an individual item based on a unique key

    /// </summary>

    /// <param name="itemId">unique key</param>

    /// <returns>Details about the Item</returns>

    public ItemInfo GetItem(string itemId)

    {

    }

 

(2)     单行注释

    //

    // 注释语句

    //

    private int number;

       

    // 注释语句

    private int number;

 

    if ( 1 == 1) // always true

    {

        statement;

    } // always true

 

 

posted @ 2013-04-06 13:08  阳光小屋  阅读(281)  评论(1编辑  收藏  举报