C#命名规则和编码规范

2017-02-10  本文已影响199人  木心Sepith
 public class HelloWorld
 {
     public void SayHello(string name)
     {
     }
 }

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

 public class Product
 {
     private string productId;
     private string productName;

     public void AddProduct(string productId,string productName)
     {
     }
 }

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

 // Correct
 public static const string ShippingType = "DropShip";

 // Avoid
 public static const string SHIPPINGTYPE = "DropShip";
 public interface IConvertible
 {
     byte ToByte();
 }
 public class TableAttribute:Attribute
 {

 }
 public class NullEmptyException:Exception
 {

 }
 public class Employee
 {
 }
 public class BusinessLocation
 {
 }
 public class DocumentCollection
 {
 }
public class File
 {
     public void CreateFile(string filePath)
     {
     }
     public void GetPath(string path)
     {
     }
 }
public Sample()
{
   // TODO: 在此处添加构造函数逻辑
}
Label --> lbl、Text --> txt、Button --> btn
Image --> img、 Widget --> Wgt、 List --> lst、CheckBox --> chk
Hyperlink --> lnk、Panel --> pnl、Table --> tab
ImageButton --> imb
// 不友好的写法
private bool isFinished = true;
if(isFinished == true)
{
    // ...
}
// 正确的写法
private bool isFinished = true;
if(isFinished)
{
    // ...
}
// 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()
    {
        // ...
    }
}

参考文章:
http://www.dofactory.com/reference/csharp-coding-standards
https://blogs.msdn.microsoft.com/brada/2005/01/26/internal-coding-guidelines/

上一篇 下一篇

猜你喜欢

热点阅读