給 c# 程式員的十個重要提示(轉)

來源:互聯網
上載者:User

標籤:http   io   ar   使用   sp   for   on   問題   cti   

本文講述我認為對 c# 程式員最重要的 10 個提示, 每個提示都會有一段對應的代碼, 對 新手來說也很容易掌握。

1: 為非公開的方法編寫測試

你嘗試過為組件的非公開方法寫測試嗎? 很多開發人員都沒有寫過, 因為這些方法對測試項 目來說是不可見的。 c# 可以通過在AssemblyInfo.cs 中添加下面的標記 (InternalsVisibleToAttribute) , 讓內部成員對其它組件可見。

//Make the internals visible to the test assembly[assembly: InternalsVisibleTo("MyTestAssembly")]
2: 使用 Tuples 類型

曾經見到過有人僅僅因為函數要返回多個值而建立了一個 POCO 類別, 其實 .Net 4.0 中的 Tuples 類型會更加適用, 例如:

public Tuple<int, string, string> GetEmployee() {    int employeeId = 1001;    string firstName = "Rudy";    string lastName = "Koertson";    //Create a tuple and return    return Tuple.Create(employeeId, firstName, lastName);}
3: 用 yield 替代臨時集合

從集合中選出部分成員時, 通常會建立一個臨時集合/列表來儲存成員並返回, 例如下面 的代碼:

public List<int> GetValuesGreaterThan100(List<int> masterCollection) {    List<int> tempResult = new List<int>();    foreach (var value in masterCollection) {        if (value > 100) {            tempResult.Add(value);        }    }    return tempResult;}

要避免這樣的臨時集合, 可以使用 yield 關鍵字, 樣本如下:

public IEnumerable<int> GetValuesGreaterThan100(List<int> masterCollection) {    foreach (var value in masterCollection) {        if (value > 100) {            yield return value;        }    }}

當然, 也可是使用 LINQ 來解決上面的問題。

4: 告訴別人你將替換一個方法

當你有一個組件時, 並且你打算替換其中的一個方法時, 可以先為方法添加過時標記以通 知用戶端, 範例程式碼如下:

[Obsolete("This method will be deprecated soon. You could use XYZ alternatively.")]public void MyComponentLegacyMethod() {    //Here is the implementation}

使用這個方法用戶端在編譯時間會發出一個警告, 如果你不再允許用戶端使用過時的方法時, 可以為過時標記添加一個額外的布爾參數, 在下面的例子中, 客戶但程式將編譯失敗:

[Obsolete("This method is deprecated. You could use XYZ alternatively.", true)]public void MyComponentLegacyMethod() {    //Here is the implementation}
5: 牢記 LINQ 查詢是順延強制的

在 .NET 中編寫 LINQ 查詢時, 只有當你訪問 LINQ 查詢的結果時, LINQ 查詢才會被執 行, LINQ 的這種特徵被稱為順延強制, 不過值得注意的是每訪問一次結果, LINQ 查詢 都會被執行一次。

為了避免重複 LINQ 查詢的重複執行, 可以先將查詢轉換成列表, 如下所示:

public void MyComponentLegacyMethod(List<int> masterCollection) {    // 在下面樣本中, 如果沒有調用 ToList , LINQ 查詢將會被執行兩次    var result = masterCollection.Where(i => i > 100).ToList();    Console.WriteLine(result.Count());    Console.WriteLine(result.Average());}
6: 使用 explicit 關鍵字轉換業務實體類型

使用 explicit 關鍵字來定義業務實體類型之間的轉換, 當代碼中出現類型轉換請求時, 轉換方法會自動執行, 下面是範例程式碼:

class Program {    static void Main(string[] args) {        var entity = new ExternalEntity {            Id = 1001,            FirstName = "Dave",            LastName = "Johnson"        };        var convertedEntity = (MyEntity)entity;    }}class MyEntity {    public int Id { get; set; }    public string FullName { get; set; }    public static explicit operator MyEntity(ExternalEntity externalEntity) {        return new MyEntity {            Id = externalEntity.Id,            FullName = externalEntity.FirstName + " " + externalEntity.LastName        };    }}class ExternalEntity {    public int Id { get; set; }    public string FirstName { get; set; }    public string LastName { get; set; }}
7: 保持異常的原始堆疊追蹤

在 c# 代碼中, 如果你像下面的代碼一樣在 catch 代碼塊中拋出 ConnectDatabase 方法中出現的異常, 異常的堆棧就會只顯示到RunDataOperation 方法, 這樣就會丟失 異常原始的堆疊追蹤資訊導致不能找到確切的錯誤源頭。

public void RunDataOperation() {    try {        Intialize();        ConnectDatabase();        Execute();    }    catch (Exception exception) {        throw exception;    }}

保持原始堆疊追蹤的代碼如下:

public void RunDataOperation() {    try {        Intialize();        ConnectDatabase();        Execute();    }    catch (Exception) {        throw;    }}
8: 使用 Flags 標記將枚舉作為位域處理

在 c# 中為枚舉類型添加 Flags 標記可以將枚舉作為位域(即一組標誌)處理, 這樣可 以對枚舉值進行自由組合, 範例程式碼如下:

class Program {    static void Main(string[] args) {        int snakes = 14;        Console.WriteLine((Reptile)snakes);    }}[Flags]enum Reptile {    BlackMamba = 2,    CottonMouth = 4,    Wiper = 8,    Crocodile = 16,    Aligator = 32}

上面代碼的輸出為 “BlackMamba, CottonMouth, Wiper” , 如果沒有 Flags 標記, 則上 面的輸出為 14 。

9: 為泛型添加類型約束

建立泛型型別時, 需要指定提供的泛型型別必須實現指定的參數或者繼承自特定的基類時, 可以這樣做:

class MyGenricClass<T> where T : IMyInterface {    //Body of the class come in here}

當然, 也可以在方法層級這樣做:

class MyGenricClass {    public void MyGenericMethod<T>(T t) where T : IMyInterface {        //Generic implementation goes in here    }}
10: IEnumerable 類型不能確保唯讀

在你建立的類型中, 暴露了一個類型為 IEnumerable 的唯讀屬性, 但是調用者依然可 以通過類型轉換來修改屬性的內容, 比如這樣:

class Program {    static void Main(string[] args) {        MyClass myClass = new MyClass();        ((List<string>)myClass.ReadOnlyNameCollection).Add("######From Client#####");        myClass.Print();    }}class MyClass {    List<string> _nameCollection = new List<string>();    public MyClass() {        _nameCollection.Add("Rob");        _nameCollection.Add("John");        _nameCollection.Add("Jummy");        _nameCollection.Add("Derek");    }    public IEnumerable<string> ReadOnlyNameCollection {        get { return _nameCollection.AsEnumerable(); }    }    public void Print() {        foreach (var item in ReadOnlyNameCollection) {            Console.WriteLine(item);        }    }}

上面的代碼修改了列表, 添加了一個新項目, 要避免這種情況, 應使用 AsReadOnly 而不是 AsEnumerable :

public IEnumerable<string> ReadOnlyNameCollection {    get { return _nameCollection.AsReadOnly(); }}

希望這些提示對你有用!

原文地址: Top 10 Tips for C# Programmers

給 c# 程式員的十個重要提示(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.