日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

给 c# 程序员的十个重要提示

發布時間:2025/3/17 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给 c# 程序员的十个重要提示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

本文講述我認為對 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


轉載于:https://my.oschina.net/lichaoqiang/blog/605942

總結

以上是生活随笔為你收集整理的给 c# 程序员的十个重要提示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。