微軟在Visual Studio 2015中更新C#語言到6.0,添加了很多很好的特性,以使C#語言繼續躋身於最優秀語言之行列。下面通過一個例子快速感受一下C# 6.0的新特性,以下程序在VS2015正式版中測試通過。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CSharp6Research 8 { 9 //分數 10 public class Fraction 11 { 12 public int A { get; set; } 13 14 public int B { get; set; } = 1; 15 16 public string Separator { get; } = "/"; 17 18 public string SeparatorSpaces { get; } = string.Empty; 19 20 public double Value => (double)A / B; 21 22 public int this[int index] => index == 0 ? A : B; 23 24 public int this[string index] => index == "A" ? A : B; 25 26 public override string ToString() => $"{A}{SeparatorSpaces}{Separator}{SeparatorSpaces}{B}"; 27 28 public void Print() => Console.WriteLine(ToString()); 29 30 public Fraction() 31 { 32 33 } 34 35 public Fraction(int a, int b) 36 { 37 A = a; 38 B = b; 39 } 40 41 public Fraction(int a, int b, string separatorSpaces) : this(a, b) 42 { 43 SeparatorSpaces = separatorSpaces; 44 if (string.IsNullOrEmpty(separatorSpaces)) 45 { 46 throw new ArgumentNullException(nameof(separatorSpaces)); 47 } 48 } 49 50 public static readonly Dictionary<string, Fraction> CommonFractions = 51 new Dictionary<string, Fraction> 52 { 53 ["zero"] = new Fraction(), 54 ["one"] = new Fraction(1, 1), 55 ["half"] = new Fraction(1, 2), 56 ["quarter"] = new Fraction(1, 4), 57 ["infinity"] = new Fraction(1, 0), 58 }; 59 60 } 61 62 public struct FractionStruct 63 { 64 public int A { get; } 65 public int B { get; } 66 public FractionStruct(int a, int b) { A = a; B = b; } 67 68 public override string ToString() => $"{A}/{B}"; 69 70 } 71 72 class Program 73 { 74 static void Main(string[] args) 75 { 76 foreach (var f in Fraction.CommonFractions) 77 { 78 Console.WriteLine($"{f.Key} : {f.Value.Value}"); 79 } 80 81 var fraction = new Fraction(1, 3, " "); 82 fraction.Print(); 83 84 try 85 { 86 fraction = new Fraction(1, 2, null); 87 } 88 catch (ArgumentNullException e) when (e.ParamName == "separatorSpaces") 89 { 90 Console.WriteLine("separatorSpaces can not be null"); 91 } 92 93 Fraction v; 94 Fraction.CommonFractions.TryGetValue("harf", out v); 95 v?.Print(); 96 var a = v?.A; 97 Console.WriteLine(a == null); 98 var b = v?["B"]; 99 Console.WriteLine(b == null); 100 Console.WriteLine(v?.ToString() == null); 101 102 Console.WriteLine(new FractionStruct(0, 1).ToString()); 103 Console.WriteLine(default(FractionStruct).ToString()); 104 } 105 106 } 107 }
運行結果如下,
zero : 0 one : 1 half : 0.5 quarter : 0.25 infinity : ∞ 1 / 3 separatorSpaces can not be null True True True 0/1 0/0
1. Auto-property initializers 自動屬性初始化器
public int B { get ; set ; } = 1;
可以直接給自動屬性賦值了,不需要寫在構造函數中了。
2. Getter-only auto-properties 只讀自動屬性
public string SeparatorSpaces { get; } = string.Empty;
只讀自動屬性可以直接初始化,或者在構造函數中初始化。
3. Expression-bodied members 表達式體成員
public double Value => (double)A / B;
public int this[int index] => index == 0 ? A : B;
public void Print() => Console.WriteLine(ToString());
只讀屬性,只讀索引器和方法都可以使用Lambda表達式作為Body。
4. String interpolation 字符串嵌入值
$"{A}{SeparatorSpaces}{Separator}{SeparatorSpaces}{B}";
字符串前加$,大括號中的表達式會在運行時計算值,並嵌入到字符串中。
5. nameof operator nameof 運算符
throw new ArgumentNullException(nameof(separatorSpaces));
nameof會返回變量,參數或成員名。
這個很有用,原來寫WPF中的ViewModel層的屬性變化通知時,需要寫字符串,或者使用MvvmLight等庫中的幫助方法,可以傳入獲取屬性的Lambda,但由於是在運行時解析,會有少許性能損失。現在好了,使用nameof運算符,既能保證重構安全和可讀性,又能保證性能。
6. Dictionary initializer 字典初始化器
new Dictionary< string, Fraction>
{
["zero"] = new Fraction (),
["one"] = new Fraction (1, 1),
["half"] = new Fraction (1, 2),
["quarter"] = new Fraction (1, 4),
[ "infinity"] = new Fraction (1, 0),
};
現在字典可以用一種可讀性更好的方法進行初始化,方括號包圍的Key等於Value。
7. Exception filters 異常過濾器
catch (ArgumentNullException e) when (e.ParamName == "separatorSpaces")
{
Console.WriteLine("separatorSpaces can not be null");
}
設置進入catch塊的條件。
8. Null propagation 空傳播
v?.A
v?["B"]
v?.ToString()
對象為null時不調用屬性,索引器,方法等,表達式返回null,和Swift中的用法相似。
9. Await in catch/finally catch和finally塊中的await
例子如下,
Resource res = null;
try
{
res = await Resource.OpenAsync(…); // You could do this.
…
}
catch (ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this …
}
finally
{
if (res != null) await res.CloseAsync(); // … and this.
}