欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

給 c# 程序員的十個重要提示

 更新時間:2021年02月26日 08:51:11   作者:張志敏  
這篇文章主要介紹了給 c# 程序員的十個重要提示,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

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

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

你嘗試過為組件的非公開方法寫測試么? 很多開發(fā)者都沒有寫過, 因?yàn)檫@些方法對測試項(xiàng) 目來說是不可見的。 c# 可以通過在 AssemblyInfo.cs 中添加下面的標(biāo)記 (InternalsVisibleToAttribute) , 讓內(nèi)部成員對其它組件可見。

//Make the internals visible to the test assembly
[assembly: InternalsVisibleTo("MyTestAssembly")]

2: 使用 Tuples 類型

曾經(jīng)見到過有人僅僅因?yàn)楹瘮?shù)要返回多個值而創(chuàng)建了一個 POCO 類, 其實(shí) .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 替代臨時集合

從集合中選出部分成員時, 通常會創(chuàng)建一個臨時集合/列表來保存成員并返回, 例如下面 的代碼:

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 關(guān)鍵字, 示例如下:

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

當(dāng)然, 也可是使用 LINQ 來解決上面的問題。

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

當(dāng)你有一個組件時, 并且你打算替換其中的一個方法時, 可以先為方法添加過時標(biāo)記以通 知客戶端, 示例代碼如下:

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

使用這個方法客戶端在編譯時會發(fā)出一個警告, 如果你不再允許客戶端使用過時的方法時, 可以為過時標(biāo)記添加一個額外的布爾參數(shù), 在下面的例子中, 客戶但程序?qū)⒕幾g失?。?/p>

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

5: 牢記 LINQ 查詢是延遲執(zhí)行的

在 .NET 中編寫 LINQ 查詢時, 只有當(dāng)你訪問 LINQ 查詢的結(jié)果時, LINQ 查詢才會被執(zhí) 行, LINQ 的這種特征被稱為延遲執(zhí)行, 不過值得注意的是每訪問一次結(jié)果, LINQ 查詢 都會被執(zhí)行一次。

為了避免重復(fù) LINQ 查詢的重復(fù)執(zhí)行, 可以先將查詢轉(zhuǎn)換成列表, 如下所示:

public void MyComponentLegacyMethod(List<int> masterCollection) {
 // 在下面示例中, 如果沒有調(diào)用 ToList , LINQ 查詢將會被執(zhí)行兩次
 var result = masterCollection.Where(i => i > 100).ToList();
 Console.WriteLine(result.Count());
 Console.WriteLine(result.Average());
}

6: 使用 explicit 關(guān)鍵字轉(zhuǎn)換業(yè)務(wù)實(shí)體類型

使用 explicit 關(guān)鍵字來定義業(yè)務(wù)實(shí)體類型之間的轉(zhuǎn)換, 當(dāng)代碼中出現(xiàn)類型轉(zhuǎn)換請求時, 轉(zhuǎn)換方法會自動執(zhí)行, 下面是示例代碼:

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 方法中出現(xiàn)的異常, 異常的堆棧就會只顯示到 RunDataOperation 方法, 這樣就會丟失 異常原始的堆棧跟蹤信息導(dǎo)致不能找到確切的錯誤源頭。

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

保持原始堆棧跟蹤的代碼如下:

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

8: 使用 Flags 標(biāo)記將枚舉作為位域處理

在 c# 中為枚舉類型添加 Flags 標(biāo)記可以將枚舉作為位域(即一組標(biāo)志)處理, 這樣可 以對枚舉值進(jìn)行自由組合, 示例代碼如下:

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 標(biāo)記, 則上 面的輸出為 14 。

9: 為泛型添加類型約束

創(chuàng)建泛型類型時, 需要指定提供的泛型類型必須實(shí)現(xiàn)指定的參數(shù)或者繼承自特定的基類時, 可以這樣做:

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

當(dāng)然, 也可以在方法級別這樣做:

class MyGenricClass {

 public void MyGenericMethod<T>(T t) where T : IMyInterface {
  //Generic implementation goes in here
 }

}

10: IEnumerable 類型不能確保只讀

在你創(chuàng)建的類型中, 暴露了一個類型為 IEnumerable 的只讀屬性, 但是調(diào)用者依然可 以通過類型轉(zhuǎn)換來修改屬性的內(nèi)容, 比如這樣:

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);
  }
 }
}

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

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

以上就是給 c# 程序員的十個重要提示的詳細(xì)內(nèi)容,更多關(guān)于c# 程序員的十個重要提示的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論