博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#命名规则和编码规范
阅读量:2120 次
发布时间:2019-04-30

本文共 2560 字,大约阅读时间需要 8 分钟。

1. 用Pascal规则来命名属性、方法、事件和类名

 

1

2

3

4

5

6

 

public class HelloWorld

{

public void SayHello(string name)

{

}

}

Pascal规则是指名称中单词的首字母大写 ,如EmployeeSalary、 ConfimationDialog、PlainTextEncoding。

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

 

1

2

3

4

5

6

7

8

9

 

public class Product

{

private string productId;

private string productName;

public void AddProduct(string productId,string productName)

{

}

}

Camel规则类似于Pascal规则 ,但名称中第一个单词的首字母不大写 ,如employeeSalary、 confimationDialog、plainTextEncoding。

3. 不要使用匈牙利命名法

不要给成员变量加任何前缀(如、m、s_等等)。如果想要区分局部变量和成员变量,可以使用this关键字。

4. 不要将常量或者只读变量的变量名全部大写,而使用Pascal规则来命名

 

1

2

3

4

5

 

// Correct

public static const string ShippingType = "DropShip";

// Avoid

public static const string SHIPPINGTYPE = "DropShip";

5. 接口的名称一般以大写I作前缀

 

1

2

3

4

 

public interface IConvertible

{

byte ToByte();

}

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

 

1

2

3

 

public class TableAttribute:Attribute

{

}

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

 

1

2

3

4

 

public class NullEmptyException:Exception

{

}

8. 类的命名。用名词或名词短语来命名类名

 

1

2

3

4

5

6

7

8

9

 

public class Employee

{

}

public class BusinessLocation

{

}

public class DocumentCollection

{

}

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

 

1

2

3

4

5

6

7

8

9

 

public class File

{

public void CreateFile(string filePath)

{

}

public void GetPath(string path)

{

}

}

10. 局部变量的名称要有意义

不要直接用用i,j,k,l,m,n,x,y,z等做变量名,但for循环除外

11. 代码分块

所有的成员变量声明在类的顶端,用一个换行把它和方法分开。同时可以使用成对的#region...#endregion标记,方便折叠。

12. 布尔型变量或者方法一般可以用iscanhas或者should做前缀。如,isFinished, canWork等。

13. 一般 C# 的编码风格要求花括号{
另起一行,不要直接跟在类名和方法后面。

 

1

2

3

4

 

public Sample()

{

// TODO: 在此处添加构造函数逻辑

}

14. 可以用缩写作为UI元素的前缀

常见UI组件的一般缩写形式:

 

1

2

3

4

 

Label --> lbl、Text --> txt、Button --> btn

Image --> img、 Widget --> Wgt、 List --> lst、CheckBox --> chk

Hyperlink --> lnk、Panel --> pnl、Table --> tab

ImageButton --> imb

 

15. 判断条件是一个布尔变量时不要使用==进行条件判断

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

 

// 不友好的写法

private bool isFinished = true;

if(isFinished == true)

{

// ...

}

// 正确的写法

private bool isFinished = true;

if(isFinished)

{

// ...

}

```

### 16. 慎用缩写

变量名是一个单词的尽量不要缩写,多单词组成的变量名可适当缩写。

### 17. 在类的顶部声明所有的成员变量,静态变量声明在最前面

```csharp

// Correct

public class Account

{

public static string BankName;

public static decimal Reserves;

public string Number {get; set;}

public DateTime DateOpened {get; set;}

public DateTime DateClosed {get; set;}

public decimal Balance {get; set;}

// Constructor

public Account()

{

// ...

}

}

18. 方法的书写规范

如果一个方法超过25行,就需要考虑是否可以重构和拆分成多个方法。方法命名要见名知意,好的方法名可以省略多余的注释。方法功能尽量单一。

19. 其他

补充两条:

  1. 方法名中尽量少用get,表示取数据的函数直接描述行为,如MsgFromFile()
  2. 尽量少用set,应用属性替代

参考文章:

from: 

转载地址:http://tlwrf.baihongyu.com/

你可能感兴趣的文章
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>
C++/C 宏定义(define)中# ## 的含义 宏拼接
查看>>
Git安装配置
查看>>
linux中fork()函数详解
查看>>
C语言字符、字符串操作偏僻函数总结
查看>>
Git的Patch功能
查看>>
分析C语言的声明
查看>>
TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>