成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#編碼標準66條

開發 后端
本文總結了66條的C#編碼標準,供大家參考。

1.  避免將多個類放在一個文件里面。

2.  一個文件應該只有一個命名空間,避免將多個命名空間放在同一個文件里面。

3.  一個文件***不要超過500行的代碼(不包括機器產生的代碼)。

4.  一個方法的代碼長度***不要超過25行。

5.  避免方法中有超過5個參數的情況。使用結構來傳遞多個參數。

6.  每行代碼不要超過80個字符。

7.  不要手工的修改機器產生的代碼。

a)  如果需要編輯機器產生的代碼,編輯格式和風格要符合該編碼標準。

b)  Use partial classes whenever possible to factor out the maintained portions.

8.  避免利用注釋解釋顯而易見的代碼。

a)  代碼應該可以自解釋。好的代碼由可讀的變量和方法命名因此不需要注釋。

9.  Document only operational assumptions, algorithm insights and so on.  

10.  避免使用方法級的文檔。

a)  使用擴展的API文檔說明之。

b)  只有在該方法需要被其他的開發者使用的時候才使用方法級的注釋。(在C#中就是///)

11.  不要硬編碼數字的值,總是使用構造函數設定其值。

12.  只有是自然結構才能直接使用const,比如一個星期的天數。

13.  避免在只讀的變量上使用const。如果想實現只讀,可以直接使用readonly。

  1. public class MyClass   
  2.  
  3. {   
  4.  
  5.    public readonly int Number;   
  6.  
  7.    public MyClass(int  someValue)   
  8.  
  9.    {   
  10.  
  11.       Number = someValue;   
  12.  
  13.    }   
  14.  
  15.    public  const int  DaysInWeek = 7;   
  16.  
  17. }   
  18.  

14.  每個假設必須使用Assert檢查

a)  平均每15行要有一次檢查(Assert)

  1. using System.Diagnostics;   
  2.  
  3. object GetObject()   
  4.  
  5. {…}   
  6.  
  7. object obj = GetObject();   
  8.  
  9. Debug.Assert(obj != null);   

15.  代碼的每一行都應該通過白盒方式的測試。

16.  只拋出已經顯示處理的異常。

17.  在捕獲(catch)語句的拋出異常子句中(throw),總是拋出原始異常維護原始錯誤的堆棧分配。

  1. catch(Exception exception)   
  2.  
  3. {      
  4.  
  5.    MessageBox.Show(exception.Message);   
  6.  
  7.    throw ;  //和throw exception一樣。   
  8.  
  9. }   
  10.  

18.  避免方法的返回值是錯誤代碼。

19.  盡量避免定義自定義異常類。

20.  當需要定義自定義的異常時:

a)  自定義異常要繼承于ApplicationException。

b)  提供自定義的序列化功能。

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.  避免給枚舉變量提供顯式的值。

  1. //正確方法    
  2.  
  3. public enum Color   
  4.  
  5. {      
  6.  
  7.    Red,Green,Blue   
  8.  
  9. }   
  10.  
  11. //避免  
  12.  
  13. public enum Color   
  14.  
  15. {      
  16.  
  17.    Red = 1,Green =  2,Blue = 3   
  18.  
  19. }   
  20.  

27.  避免指定特殊類型的枚舉變量。

  1. //避免    
  2.  
  3. public enum Color  : long   
  4.  
  5. {      
  6.  
  7.    Red,Green,Blue   
  8.  
  9. }   
  10.  

28.  即使if語句只有一句,也要將if語句的內容用大括號擴起來。

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

30.  避免在條件語句中調用返回bool值的函數。可以使用局部變量并檢查這些局部變量。

  1. bool IsEverythingOK()   
  2.  
  3. {…}   
  4.  
  5. //避免   
  6.  
  7. if (IsEverythingOK ())   
  8.  
  9. {…}   
  10.  
  11. //替換方案    
  12.  
  13. bool ok = IsEverythingOK();   
  14.  
  15. if (ok)   
  16.  
  17. {…}   
  18.  

31.  總是使用基于0開始的數組。

32.  在循環中總是顯式的初始化引用類型的數組。

  1. public class MyClass   
  2.  
  3. {}   
  4.  
  5. MyClass[] array = new  MyClass[100];   
  6.  
  7. for(int index = 0; index <  array.Length;  index++)   
  8.  
  9. {   
  10.  
  11.    array[index] = new  MyClass();   
  12.  
  13. }   
  14.  

33.  不要提供public 和 protected的成員變量,使用屬性代替他們。

34.  避免在繼承中使用new而使用override替換。

35.  在不是sealed的類中總是將public 和 protected的方法標記成virtual的。

36.  除非使用interop(COM+ 或其他的dll)代碼否則不要使用不安全的代碼(unsafe code)。

37.  避免顯示的轉換,使用as操作符進行兼容類型的轉換。

  1. Dog dog = new GermanShepherd();   
  2.  
  3. GermanShepherd shepherd = dog  as  GermanShepherd;   
  4.  
  5. if (shepherd != null )   
  6.  
  7. {…}   
  8.  

38.  當類成員包括委托的時候

a)  Copy a delegate to a local variable before publishing to avoid concurrency race

condition. 

b)  在調用委托之前一定要檢查它是否為null

  1. public class MySource   
  2.  
  3. {   
  4.  
  5.    public event EventHandler  MyEvent;   
  6.  
  7.    public void FireEvent()   
  8.  
  9.    {   
  10.  
  11.       EventHandler temp = MyEvent;   
  12.  
  13.       if(temp != null )   
  14.  
  15.       {   
  16.  
  17.          temp(this,EventArgs.Empty);   
  18.  
  19.       }   
  20.  
  21.    }   
  22.  
  23. }     
  24.  

39.  不要提供公共的事件成員變量,使用事件訪問器替換這些變量。

  1. public class MySource   
  2.  
  3. {   
  4.  
  5.    MyDelegate m_SomeEvent ;   
  6.  
  7.    public event MyDelegate SomeEvent   
  8.  
  9.    {   
  10.  
  11.       add   
  12.  
  13.       {   
  14.  
  15.          m_SomeEvent += value;   
  16.  
  17.       }   
  18.  
  19.       remove   
  20.  
  21.       {   
  22.  
  23.          m_SomeEvent -= value;   
  24.  
  25.       }   
  26.  
  27.    }   
  28.  
  29. }   
  30.  

40.  使用一個事件幫助類來公布事件的定義。

41.  總是使用接口。

42.  類和接口中的方法和屬性至少為2:1的比例。

43.  避免一個接口中只有一個成員。

44.  盡量使每個接口中包含3-5個成員。

45.  接口中的成員不應該超過20個。

a)  實際情況可能限制為12個

46.  避免接口成員中包含事件。

47.  避免使用抽象方法而使用接口替換。

48.  在類層次中顯示接口。

49.  推薦使用顯式的接口實現。

50.  從不假設一個類型兼容一個接口。Defensively query for that interface.

SomeType obj1;

IMyInterface obj2;

  1. /* 假設已有代碼初始化過obj1,接下來 */   
  2.  
  3. obj2 = obj1 as IMyInterface;   
  4.  
  5. if (obj2 != null)   
  6.  
  7. {   
  8.  
  9.    obj2.Method1();   
  10.  
  11. }   
  12.  
  13. else   
  14.  
  15. {   
  16.  
  17.    //處理錯誤   
  18.  
  19. }     
  20.  

51.  表現給最終用戶的字符串不要使用硬編碼而要使用資源文件替換之。

52.  不要硬編碼可能更改的基于配置的字符串,比如連接字符串。

53.  當需要構建長的字符串的時候,使用StringBuilder不要使用string

54.  避免在結構里面提供方法。

a)  建議使用參數化構造函數

b)  可以重裁操作符

55.  總是要給靜態變量提供靜態構造函數。

56.  能使用早期綁定就不要使用后期綁定。

57.  使用應用程序的日志和跟蹤。

58.  除非在不完全的switch語句中否則不要使用goto語句。

59.  在switch語句中總是要有default子句來顯示信息(Assert)。

  1. int number  = SomeMethod();   
  2.  
  3. switch(number)   
  4.  
  5. {   
  6.  
  7.    case 1:   
  8.  
  9.       Trace.WriteLine("Case 1:");   
  10.  
  11.       break;   
  12.  
  13.    case 2:   
  14.  
  15.       Trace.WriteLine("Case 2:");   
  16.  
  17.       break;   
  18.  
  19.    default :   
  20.  
  21.       Debug.Assert(false);   
  22.  
  23.       break;   
  24.  
  25. }   
  26.  

60.  除非在構造函數中調用其他構造函數否則不要使用this指針。

  1. // 正確使用this的例子   
  2.  
  3. public class MyClass   
  4.  
  5. {   
  6.  
  7.    public MyClass(string message )   
  8.  
  9.    {}   
  10.  
  11.    public MyClass()  : this("hello")   
  12.  
  13.    {}   
  14.  
  15. }   
  16.  

61.  除非你想重寫子類中存在名稱沖突的成員或者調用基類的構造函數否則不要使用base來訪問基類的成員。

  1. // 正確使用base的例子  
  2.  
  3. public class Dog   
  4.  
  5. {   
  6.  
  7.    public Dog(string name)   
  8.  
  9.    {}   
  10.  
  11.    virtual public void Bark( int howLong)   
  12.  
  13.    {}   
  14.  
  15. }   
  16.  
  17. public class GermanShepherd : Dog   
  18.  
  19. {   
  20.  
  21.    public GermanShe pherd(string name): base (name)   
  22.  
  23.    {}   
  24.  
  25.    override public void Bark(int  howLong)    
  26.  
  27.    {   
  28.  
  29.       base .Bark(howLong);     
  30.  
  31.    }   
  32.  
  33. }   
  34.  

62.  基于模板的時候要實現Dispose()和Finalize()兩個方法。

63.  通常情況下避免有從System.Object轉換來和由System.Object轉換去的代碼,而使用強制轉換或者as操作符替換。

  1. class SomeClass   
  2.  
  3. {}   
  4.  
  5. //避免:   
  6.  
  7. class MyClass< T>    
  8.  
  9. {      
  10.  
  11.    void SomeMethod(T t)      
  12.  
  13.    {   
  14.  
  15.       object temp = t;         
  16.  
  17.       SomeClass obj = (SomeClass)temp;       
  18.  
  19.    }   
  20.  
  21. }   
  22.  
  23. // 正確:   
  24.  
  25. class MyClass< T> where T : SomeClass   
  26.  
  27. {      
  28.  
  29.    void SomeMethod(T t)      
  30.  
  31.    {   
  32.  
  33.       SomeClass obj = t;      
  34.  
  35.    }   
  36.  
  37. }   
  38.  

64.  在一般情況下不要定影有限制符的接口。接口的限制級別通常可以用強類型來替換之。

  1. public class Customer   
  2.  
  3. {…}   
  4.  
  5. //避免:  
  6.  
  7. public interface IList< T> where T : Customer    
  8.  
  9. {…}   
  10.  
  11. //正確:  
  12.  
  13. public interface ICustomerList : IList< Customer>    
  14.  
  15. {…}   
  16.  

65.  不確定在接口內的具體方法的限制條件。

66.  總是選擇使用C#內置(一般的generics)的數據結構。

以上就是總結的66條C#編碼標準。

【編輯推薦】

  1. C#自定義控件的開發:Pin和Connector
  2. 比較C#自定義控件的property(屬性)和/attribute(性質)
  3. C#組件開發:COM和.NET對象之間的互操作
  4. 介紹.NET平臺、C#和ASP.NET
  5. C# Attributes:定義設計期信息
責任編輯:book05 來源: 網易博客
相關推薦

2009-09-01 17:04:47

C#編碼標準

2009-08-03 16:22:58

C#編程技巧

2009-08-25 17:46:50

C#生成漢字編碼原理

2011-03-25 09:08:49

C#

2009-08-17 14:41:47

C#進度條實現

2009-08-18 09:49:00

C# listview

2009-08-17 15:48:47

C# WinForm進

2009-06-24 10:49:16

JavaScript

2012-03-08 15:03:49

JavaScript

2011-03-29 09:14:49

Dispose模式C#

2021-04-09 10:01:47

微軟開源C#

2009-08-17 17:15:48

C# 進度條效果

2009-08-17 14:36:15

C#進度條實現

2009-08-17 13:56:29

C#進度條的使用

2009-08-25 17:15:50

C#隱藏C#重寫C#重載

2009-08-17 15:05:41

C#進度條

2009-09-02 17:10:45

C#語言入門

2009-08-25 17:21:31

C#索引

2020-07-02 15:15:22

JavaScript面試前端

2013-05-27 10:14:21

PHPZend FramewPHP編碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 午夜a√ | 国产精品久久久久久久久久久久久 | 日韩精品视频一区二区三区 | 国产一区二区三区高清 | 欧美久久久 | 色资源在线视频 | 日韩一区二区三区四区五区 | 国产亚洲一区二区三区 | 色播久久| 欧美中文一区 | 国产精品一区久久久久 | 亚洲三级在线 | 在线观看国产www | 欧美性猛交一区二区三区精品 | 激情国产视频 | 成人免费视频 | 日本大片在线播放 | 精品毛片视频 | 日本一区二区三区四区 | 国产精品我不卡 | 亚洲精品视频在线观看视频 | 4h影视| av在线影院| 一区二区三区视频在线 | 男女羞羞视频网站 | 丁香婷婷久久久综合精品国产 | 日韩精品av| 国产乱码精品一区二区三区忘忧草 | 91精品在线播放 | 国产精品777一区二区 | 国产精品久久久久久久久久久久午夜片 | 久久久www成人免费精品 | 射久久 | 日日操视频 | 欧美日韩不卡合集视频 | 伊人艹 | 中文在线一区 | 国产免费一区二区 | 国产一区二区在线看 | 久久精品欧美一区二区三区麻豆 | 精品福利在线 |