20151009 C# 第一篇 程序编写规范

20151009

程序编写规范

1、 代码书写规则:

1)、尽量使用接口,然后使用类实现接口。

2)、关键语句写注释

3)、避免写超过5个参数的方法,如果要传递多个参数,则使用结构

4)、避免代码量过大的try…catch…模块

5)、避免在同一个文件中放置多个类

6)、switch 语句一定要有default语句处理意外情况

7)、生成和构建一个长字符串时,一定要使用StringBuilder类型(可变字符序列),而不使用string

8)if 语句应该使用{}包含起来。

 

2、 命名规范

1)、用 Pascal 规则来命名方法和类型

Pascal 规则:第一个字母大写,后面连接词的第一个字母也为大写

如:

Public class DataGrid

{

Public void DataBind

{

}

}

 

2)、用 Camel 规则来命名局部变量和方法的参数

Pascal 规则:第一个单词的第一个字母小写

如:

String strUserName

Public void addUser(string strUserId, byte[] byPassword);

3)、所有的成员变量前加前缀“_

如:在公共类DataBase中声明一个私有成员变量

Public class DataBase

{

    Private string _connectionString;

}

4)、接口的名称前加“I

如:创建一个公共接口Iconvertible

Public interface Iconvertible

{

    Byte ToByte();

}

5)、方法的命名,一般将其命名为动宾短语

如:在公共类File中创建 CreateFileGetPath方法

Public class File

{

Public void CreateFile(string strFilePath)

{

}

Public void GetPath(string path)

{

}

}

6)、所有的成员变量声明在类的顶端

如:

Public class Product

{

Private string _productId;

Private string _productName

Public void AddProduct(string strProductId, string strProductName)

{

}

}

7)、用有意义的名字命名命名空间,如公司名、产品名

如:

Namespace Zivsoft  //公司命名

{

}

Namespace ERP    //产品命名
{

}

8)、使用某个控件值时,尽量命名局部变量

如:

Public string GetTitle()

{

String title = lbl_Title.Text;

Return title;

}

 

 

原文地址:https://www.cnblogs.com/gaoquanning/p/4864906.html