C# 6 已經出來有一段時間了,今天我們就詳細地看一下這些新的特性。
一、字串插值 (String Interpolation)
C# 6之前我們拼接字串時需要這樣
1 2 |
var Name = "Jack"; var results = "Hello" + Name; |
或者
1 2 |
var Name = "Jack"; var results = string.Format("Hello {0}", Name); |
但是C#6裡我們就可以使用新的字串插值特性
1 2 |
var Name = "Jack"; var results = $"Hello {Name}"; |
上面只是一個簡單的例子,想想如果有多個值要替換的話,用C#6的這個新特性,程式碼就會大大減小,而且可讀性比起之前大大增強
1 2 |
Person p = new Person {FirstName = "Jack", LastName = "Wang", Age = 100}; var results = string.Format("First Name: {0} LastName: {1} Age: { 2} ", p.FirstName, p.LastName, p.Age); |
有了字串插值後:
1 |
var results = $"First Name: {p.FirstName} LastName: {p.LastName} Age: {p.Age}"; |
字串插值不光是可以插簡單的字串,還可以直接插入程式碼
1 2 3 |
Console.WriteLine($"Jack is saying { new Tools().SayHello() }"); var info = $"Your discount is {await GetDiscount()}"; |
那麼如何處理多語言呢?
我們可以使用 IFormattable
下面的程式碼如何實現多語言?
1 2 3 4 |
Double remain = 2000.5; var results= $"your money is {remain:C}"; # 輸出 your money is $2,000.50 |
使用IFormattable 多語言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Program { static void Main(string[] args) { Double remain = 2000.5; var results= ChineseText($"your money is {remain:C}"); Console.WriteLine(results); Console.Read(); } public static string ChineseText(IFormattable formattable) { return formattable.ToString(null, new CultureInfo("zh-cn")); } } # 輸出 your money is ¥2,000.50 |
二、空操作符 ( ?. )
C# 6新增了一個 ?. 操作符,當一個物件或者屬性職為空時直接返回null, 就不再繼續執行後面的程式碼,在之前我們的程式碼裡經常出現 NullException, 所以我們就需要加很多Null的判斷,比如
1 2 3 4 |
if (user != null & user.Project != null && user.Project.Tasks != null && user.Project.Tasks.Count > 0) { Console.WriteLine(user.Project.Tasks.First().Name); } |
現在我們可以不用寫 IF 直接寫成如下這樣
1 |
Console.WriteLine(user?.Project?.Tasks?.First()?.Name); |
這個?. 特性不光是可以用於取值,也可以用於方法呼叫,如果物件為空將不進行任何操作,下面的程式碼不會報錯,也不會有任何輸出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Program { static void Main(string[] args) { User user = null; user?.SayHello(); Console.Read(); } } public class User { public void SayHello() { Console.WriteLine("Ha Ha"); } } |
還可以用於陣列的索引器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Program { static void Main(string[] args) { User[] users = null; List listUsers = null; // Console.WriteLine(users[1]?.Name); // 報錯 // Console.WriteLine(listUsers[1]?.Name); //報錯 Console.WriteLine(users?[1].Name); // 正常 Console.WriteLine(listUsers?[1].Name); // 正常 Console.ReadLine(); } } |
注意: 上面的程式碼雖然可以讓我們少些很多程式碼,而且也減少了空異常,但是我們卻需要小心使用,因為有的時候我們確實是需要丟擲空異常,那麼使用這個特性反而隱藏了Bug
三、 NameOf
過去,我們有很多的地方需要些硬字串,導致重構比較困難,而且一旦敲錯字母很難察覺出來,比如
1 2 3 |
if (role == "admin") { } |
WPF 也經常有這樣的程式碼
1 2 3 4 5 6 7 8 9 |
public string Name { get { return name; } set { name= value; RaisePropertyChanged("Name"); } } |
現在有了C#6 NameOf後,我們可以這樣
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public string Name { get { return name; } set { name= value; RaisePropertyChanged(NameOf(Name)); } } static void Main(string[] args) { Console.WriteLine(nameof(User.Name)); // output: Name Console.WriteLine(nameof(System.Linq)); // output: Linq Console.WriteLine(nameof(List)); // output: List Console.ReadLine(); } |
注意: NameOf只會返回Member的字串,如果前面有物件或者名稱空間,NameOf只會返回 . 的最後一部分, 另外NameOf有很多情況是不支援的,比如方法,關鍵字,物件的例項以及字串和表示式
四、在Catch和Finally裡使用Await
在之前的版本里,C#開發團隊認為在Catch和Finally裡使用Await是不可能,而現在他們在C#6裡實現了它。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Resource res = null; try { res = await Resource.OpenAsync(); // You could always do this. } catch (ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. } |
五、表示式方法體
一句話的方法體可以直接寫成箭頭函式,而不再需要大括號
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Program { private static string SayHello() => "Hello World"; private static string JackSayHello() => $"Jack {SayHello()}"; static void Main(string[] args) { Console.WriteLine(SayHello()); Console.WriteLine(JackSayHello()); Console.ReadLine(); } } |
六、自動屬性初始化器
之前我們需要賦初始化值,一般需要這樣
1 2 3 4 5 6 7 8 9 |
public class Person { public int Age { get; set; } public Person() { Age = 100; } } |
但是C# 6的新特性裡我們這樣賦值
1 2 3 4 |
public class Person { public int Age { get; set; } = 100; } |
七、只讀自動屬性
C# 1裡我們可以這樣實現只讀屬性
1 2 3 4 5 6 7 8 9 |
public class Person { private int age=100; public int Age { get { return age; } } } |
但是當我們有自動屬性時,我們沒辦法實行只讀屬性,因為自動屬性不支援readonly關鍵字,所以我們只能縮小訪問許可權
1 2 3 4 5 |
public class Person { public int Age { get; private set; } } |
但是 C#6裡我們可以實現readonly的自動屬性了
1 2 3 4 |
public class Person { public int Age { get; } = 100; } |
八、異常過濾器 Exception Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
static void Main(string[] args) { try { throw new ArgumentException("Age"); } catch (ArgumentException argumentException) when( argumentException.Message.Equals("Name")) { throw new ArgumentException("Name Exception"); } catch (ArgumentException argumentException) when( argumentException.Message.Equals("Age")) { throw new Exception("not handle"); } catch (Exception e) { throw; } } |
在之前,一種異常只能被Catch一次,現在有了Filter後可以對相同的異常進行過濾,至於有什麼用,那就是見仁見智了,我覺得上面的例子,定義兩個具體的異常 NameArgumentException 和AgeArgumentException程式碼更易讀。
九、 Index 初始化器
這個主要是用在Dictionary上,至於有什麼用,我目前沒感覺到有一點用處,誰能知道很好的使用場景,歡迎補充:
1 2 3 4 5 6 7 8 9 10 11 12 |
var names = new Dictionary { [1] = "Jack", [2] = "Alex", [3] = "Eric", [4] = "Jo" }; foreach (var item in names) { Console.WriteLine($"{item.Key} = {item.Value}"); } |
十、using 靜態類的方法可以使用 static using
這個功能在我看來,同樣是很沒有用的功能,也為去掉字首有的時候我們不知道這個是來自哪裡的,而且如果有一個同名方法不知道具體用哪個,當然經證實是使用類本身的覆蓋,但是容易搞混不是嗎?
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; using static System.Math; namespace CSharp6NewFeatures { class Program { static void Main(string[] args) { Console.WriteLine(Log10(5)+PI); } } } |
總結
上面一到八我認為都是比較有用的新特性,後面的幾個我覺得用處不大,當然如果找到合適的使用場景應該有用,歡迎大家補充。
最後,祝大家程式設計愉快。