學習並使用的 10 個 C# 特性

轉自伯樂在線web

一、 async / await

使用 async / await 模式,能夠在執行代碼塊操做的時候不會阻塞 UI 或者當前的線程。即便該操做被某些執行動做延遲了(好比一個 web 請求),async / await 模式也會繼續執行後續的代碼。express

更多關於 async / await 模式的內容,請見:https://msdn.microsoft.com/zh-cn/library/hh191443.aspx數組

二、對象 / 數組 / 集合的初始值設定項(initializers)

經過使用對象、數組、集合的初始值設定項,能夠很容易地建立類、數組和集合的實例:閉包

// 示例類
public class Employee {
    public string Name {get; set;}
    public DateTime StartDate {get; set;}
}
 
// 使用初始值設定項建立員工實例
Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()};

上述示例中的代碼在單元測試中可能很是有幫助,不過在有些狀況下也應該避免使用它,好比應該經過構造函數來進行類的實例化的時候。async

更多關於初始值設定項的內容,請見:https://msdn.microsoft.com/zh-cn/library/bb384062.aspxide

三、Lambda 表達式、謂詞委託(predicates)、委託(delegates)和閉包(closures)

這些特性在不少狀況下都是很是必要的(好比使用 Linq 的時候),請務必學習在什麼時候以及如何使用它們。函數

更多關於 Lambda 表達式、謂詞委託、委託和閉包的內容,請見:http://www.codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c工具

四、?? – null 合併運算符(Null coalescing operator)

當表達式左側不爲 null 的時候,?? 運算符返回其左側的值,不然返回其右側的值:單元測試

// 多是 null
var someValue = service.GetValue();
var defaultValue = 23
 
// 若是 someValue 是 null 的話,result 爲 23
var result = someValue ?? defaultValue;

?? 運算符能夠用於鏈式操做:學習

string anybody = parm1 ?? localDefault ?? globalDefault;

它也能夠將可空類型轉換爲非可空類型:

var totalPurchased = PurchaseQuantities.Sum(kvp => kvp.Value ?? 0);

更多關於 ?? 運算符的內容,請見:https://msdn.microsoft.com/zh-cn/library/ms173224.aspx

五、$」{x}」 – 內插字符串(String Interpolation)- C# 6

C# 6 的一個新特性,能夠經過更加高效和優雅的方式來進行字符串拼接:

// 傳統方式
var someString = String.Format("Some data: {0}, some more data: {1}", someVariable, someOtherVariable);
 
// 新的方式
var someString = $"Some data: {someVariable}, some more data: {someOtherVariable}";

也能夠在大括號中寫 C# 表達式,這讓它變得很是強大。

六、?. – null 條件運算符(Null-conditional operator) – C# 6

null 條件運算符使用起來以下所示:

// 若是 customer 或 customer.profile 或 customer.profile.age 爲 null 的話,結果都是 null
var currentAge = customer?.profile?.age;

再也不會發生 NullReferenceExceptions 了!

更多關於 ?. 運算符的內容,請見:https://msdn.microsoft.com/zh-cn/library/dn986595.aspx

七、nameof 表達式 – C# 6

新的 nameof 表達式可能看上去沒那麼重要,不過它確實也有其用武之地。在使用自動重構工具(好比 Resharper)時,你可能會須要經過參數的名字來表示它:

public void PrintUserName(User currentUser)
{
    // 在重命名 currentUser 的時候,重構工具可能會遺漏在文本中的這個變量名
    if(currentUser == null)
        _logger.Error("Argument currentUser is not provided");
 
    //...
}

如今你能夠這樣來寫:

public void PrintUserName(User currentUser)
{
    // 重構工具不會漏掉這個
    if(currentUser == null)
        _logger.Error($"Argument {nameof(currentUser)} is not provided");
 
    //...
}

更多關於 nameof 表達式的內容,請見:https://msdn.microsoft.com/zh-cn/library/dn986596.aspx

八、 屬性初始值設定項 – C# 6

你能夠經過屬性初始值設定項,在聲明一個屬性的時候指定一個初始值:

public class User
{ 
    public Guid Id { get; } = Guid.NewGuid();  
    // ...
}

使用屬性初始值設定項的一個好處,就是你沒必要聲明一個 setter 方法,從而使得該屬性成爲不可變的(immutable)。屬性初始值設定項能夠和 C# 6 的主構造函數(Primary Constructor)語法配合使用。(譯者注:Primary Constructor 語法可讓你在定義一個類的同時,在類名以後當即指定一個帶參數的構造函數)

九、as / is 運算符

is 運算符用來判斷一個實例是不是特定類型的,好比在使用反射判斷一個屬性的特性時:

if (attr is StringValidationAttribute)
{
    var att = (StringValidationAttribute) attr;
    if (att.MinLength > length || att.MinLength < length)
    {
        Console.WriteLine(att.ErrorMessage,att.DisplayName,att.MinLength,att.MaxLength);
        return false;
    }
    return true;
}

as 運算符會試圖將某個對象轉換爲某個特定類的實例。若是沒法轉換的話會返回 null:

SomeType y = x as SomeType;
if (y != null)
{
    //do stuff
}

十、yield 關鍵字

你能夠經過 yield 關鍵字來返回 IEnumerable 接口的數據項。下面的示例會返回 2 的次方(好比直到 8 次方:二、四、八、1六、3二、6四、12八、256):

public static IEnumerable<int> Power(int number, int exponent)
{
    int result = 1;
    for (int i = 0; i < exponent; i++)
    {
      result = result * number;
      yield return result;
    }
}

若是使用得當的話,yield 會變得很是強大。它使得你延遲生成序列中的對象,好比當系統不須要枚舉整個集合的時候,能夠按需中止。

相關文章
相關標籤/搜索