C# 编程规范

一、命名

  1.用pascal规则来命名方法和类型。

  public class TextBox

  {

  public void DataBind()

  {

  }

  }

  2.用camel规则来命名局部变量和方法的参数。

  string userName;

  public AddUser(string userId, byte[] password);

  3.所有的成员变量前加前缀 _

  public class Database

  {

  private string _connectionString;

  }

  4.接口的名称加前缀 I.

  interface ICompare

  {

  int compare();

  }

  5.自定义的属性以Attribute结尾

  public class AuthorAttribute : Attribute

  {

  }

  6.自定义的异常以Exception结尾

  public class AppException : Exception

  {

  }

  7.方法的命名。一般将其命名为动宾短语。

  ShowDialog()

  CreateFile()

  GetPath()

  8.代码的缩进。要用Tab,而不要用space.

  9.局部变量的名称要有意义。不要用x,y,z等等(除用于For循环变量中可使用i,j,k,l,m,n)。

  string userName

  10.所有的成员变量声明在类的顶端,用一个换行把它和方法分开。

  11.用有意义的名字命名namespace,如:产品名、公司名。

  12.建议局部变量在最接近使用它时再声明。

  13.使用某个控件的值时,尽量命名局部变量。

  14.把引用的系统的namespace和自定义或第三方的用一个换行把它们分开。

  15.文件名要能反应类的内容,最好是和类同名,一个文件中一个类或一组关连类。

  16.目录结构中要反应出namespace的层次。

  17.大括号"{"要新起一行。

  public class AuthorAttribute : Attribute

  {

  }

二、编码习惯。

  1.用C#预定义的类名,而不要用别名。

  string userName;   而不是 System.String userName;

  int number;            而不是 System.Int32;

  2.一行不要超过80个字符。

  3.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样。

  4.关键的语句(包括声明关键的变量)必须要写注释。

  5.文字常量和数字常量不要硬编码,应该用常量类或枚举代替。

  6.不准使用goto系列语句。

  7.不要声明public和protected的成员变量,应用property.

  8.不要声明public的event,应用事件访问器。

  public class Source

  {

  private EventHandler m_NumberChangeEvent;

  public event EventHandler NumberChangeEvent

  {

  add

  {

  m_NumberChangeEvent += value;

  }

  remove

  {

  m_NumberChangeEvent -= value;

  }

  }

  }

  9.类型转换的使用规则。

  Animal animal = new Dog();

  Dog dog = animal as Dog;

  if (dog != null)

  {

  }

  10.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.

  11.始终使用"{  }"包含if下的语句,即使只有一条语句。

  12.switch语句一定要有default来处理意外情况。

  13.尽量少使用三目运算符 ? : ,而要使用if语句。

  14.尽量不用使用this引用,除非是要调用类中的另一个Constructor.

  public class Person

  {

  public Person(string name)

  {

  }

  public Person() : this("Jim")

  {

  }

  }

二、编码习惯。

  1.用C#预定义的类名,而不要用别名。

  string userName;   而不是 System.String userName;

  int number;            而不是 System.Int32;

  2.一行不要超过80个字符。

  3.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样。

  4.关键的语句(包括声明关键的变量)必须要写注释。

  5.文字常量和数字常量不要硬编码,应该用常量类或枚举代替。

  6.不准使用goto系列语句。

  7.不要声明public和protected的成员变量,应用property.

  8.不要声明public的event,应用事件访问器。

  public class Source

  {

  private EventHandler m_NumberChangeEvent;

  public event EventHandler NumberChangeEvent

  {

  add

  {

  m_NumberChangeEvent += value;

  }

  remove

  {

  m_NumberChangeEvent -= value;

  }

  }

  }

  9.类型转换的使用规则。

  Animal animal = new Dog();

  Dog dog = animal as Dog;

  if (dog != null)

  {

  }

  10.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.

  11.始终使用"{  }"包含if下的语句,即使只有一条语句。

  12.switch语句一定要有default来处理意外情况。

  13.尽量少使用三目运算符 ? : ,而要使用if语句。

  14.尽量不用使用this引用,除非是要调用类中的另一个Constructor.

  public class Person

  {

  public Person(string name)

  {

  }

  public Person() : this("Jim")

  {

  }

  }

 

TraceBack:http://dotnet.chinaitlab.com/CSharp/735681.html

posted @ 2008-06-17 13:49  my favorite  阅读(263)  评论(0编辑  收藏  举报