C#编码规范(三)

21.  避免在单个程序集里使用多个Main方法。

22.  只对外公布必要的操作,其他的则为internal。

23.  Avoid friend assemblies, as it increases inter-assembly coupling.

24.  Avoid code that relies on an assembly running from a particular location.

25.  使应用程序集尽量为最小化代码(EXE客户程序)。使用类库来替换包含的商务逻辑。

26.  避免给枚举变量提供显式的值。

//正确方法 

public enum Color

{   

   Red,Green,Blue

}

//避免

public enum Color

{   

   Red = 1,Green =  2,Blue = 3

}

27.  避免指定特殊类型的枚举变量。

//避免 

public enum Color  : long

{   

   Red,Green,Blue

}

28.  即使if语句只有一句,也要将if语句的内容用大括号扩起来。

29.  避免使用trinary条件操作符。

30.  避免在条件语句中调用返回bool值的函数。可以使用局部变量并检查这些局部变量。

bool IsEverythingOK()

{…}

//避免

if (IsEverythingOK ())

{…}

//替换方案 

bool ok = IsEverythingOK();

if (ok)

{…}

posted @ 2011-05-22 11:04  临河羡鱼  阅读(104)  评论(0编辑  收藏  举报