C# 8.0 新特性
作者:webabcd
介紹
C# 8.0 新特性
- 解構(這是 C#7 的新特性,之前忘了寫了)
- ??=
- 集合的倒序索引和范圍索引
- switch 表達式
- 默認接口方法
示例
1、演示“解構”(這是 C#7 的新特性,之前忘了寫了)
DeconstructDemo.cs
/* * 本例用於演示“解構”(這是 C#7 的新特性,之前忘了寫了) */ using System; namespace CSharp8 { public class DeconstructDemo { class User { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } // 構造函數(用於通過指定的參數構造出一個對象) public User(string name, int age, string email) { Name = name; Age = age; Email = email; } // 解構函數(用於解構出對象的參數) public void Deconstruct(out string name, out string email) { name = Name; email = Email; } // 解構函數(用於解構出對象的參數),解構函數支持重載 public void Deconstruct(out string name, out int age, out string email) { name = Name; age = Age; email = Email; } } public static void Demo() { // 調用構造函數實例化 var user = new User("webabcd", 40, "webabcd@gmail.com"); // 調用解構函數,用於解析出對象的參數 (var n1, var e1) = user; Console.WriteLine($"name:{n1}, email:{e1}"); // name: webabcd, email: webabcd @gmail.com // 調用解構函數,用於解析出對象的參數 (var n2, var a2, var e2) = user; Console.WriteLine($"name:{n2}, age:{a2}, email:{e2}"); // name:webabcd, age: 40, email: webabcd @gmail.com } } }
2、演示 ??=, 集合的倒序索引和范圍索引
New1.cs
/* * C#8 新特性 * 1、??= * 2、集合的倒序索引和范圍索引 */ using System; namespace CSharp8 { public class New1 { public static void Demo() { Sample1(); Sample2(); } // 演示 ??= public static void Sample1() { // 演示 ?? string a = null; a = a ?? "a is null"; Console.WriteLine(a); // a is null // 演示 ??= string b = null; b ??= "b is null"; Console.WriteLine(b); // b is null } // 演示集合的范圍索引(支持倒序索引和范圍索引) public static void Sample2() { var ary = new string[] { // 正序索引 倒序所索引 "m1", // 0 ^5 "m2", // 1 ^4 "m3", // 2 ^3 "m4", // 3 ^2 "m5", // 4 ^1 }; // 正序索引 Console.WriteLine(ary[4]); // m5 // 倒序索引 Console.WriteLine(ary[^1]); // m5 // 范圍索引 // 1、左側不指定位置則為最開始 // 2、右側不指定位置則為最末尾 // 3、右側指定位置時,取值范圍不包括其指定的位置 Console.WriteLine(string.Join(',', ary[0..5])); // m1,m2,m3,m4,m5 Console.WriteLine(string.Join(',', ary[0..^0])); // m1,m2,m3,m4,m5 Console.WriteLine(string.Join(',', ary[..])); // m1,m2,m3,m4,m5 Console.WriteLine(string.Join(',', ary[..3])); // m1,m2,m3 Console.WriteLine(string.Join(',', ary[3..])); // m4,m5 // Range 結構體 Range range = 0..2; Console.WriteLine(string.Join(',', ary[range])); // m1,m2 } } }
3、演示 switch 表達式
SwitchExpression.cs
/* * C#8 新特性:switch 表達式 * * 注意:這里說的 C#8 的新特性是 switch 表達式(switch expression),而不是 switch 語句(switch statement) */ using System; namespace CSharp8 { public class SwitchExpression { public static void Demo() { Sample1(); Sample2(); Sample3(); Sample4(); } // 簡單演示一下 switch 表達式的使用 public static void Sample1() { string SwitchExpression(string input) => input switch { "1" => "111111", "2" => "222222", _ => "000000" // 這里的“_”相當於 switch 語句中的 default }; Console.WriteLine(SwitchExpression("1")); // 111111 Console.WriteLine(SwitchExpression("2")); // 222222 Console.WriteLine(SwitchExpression("1234")); // 000000 } class A { public string Name { get; set; } public int Age { get; set; } } // switch 表達式中的屬性匹配的示例 public static void Sample2() { string PropertyPattern(A a, int salary) => a switch { { Name: "name1" } => $"name:{a.Name}, age:{a.Age}, salary:{salary}", _ => "000000" }; Console.WriteLine(PropertyPattern(new A { Name = "name1" }, 100)); // name:name1, age:0, salary:100 Console.WriteLine(PropertyPattern(new A { Name = "name2" }, 100)); // 000000 } // switch 表達式中的元組(Tuple)匹配的示例 public static void Sample3() { string TuplePattern(string first, string second) => (first, second) switch { ("a", "b") => $"first:{first}, second:{second}", _ => "000000" }; Console.WriteLine(TuplePattern("a", "b")); // first:a, second:b Console.WriteLine(TuplePattern("c", "d")); // 000000 } class B { public string Name { get; set; } public int Age { get; set; } public void Deconstruct(out string name, out int age) { name = Name; age = Age; } } // switch 表達式中的可結構對象匹配的示例 public static void Sample4() { string PositionalPattern(B b) => b switch { ("webabcd1", 20) => "aaaaaa", var (x, y) when y > 30 => "bbbbbb", // 要支持這種寫法,則對象必須要實現相應的解構函數 _ => "000000" }; Console.WriteLine(PositionalPattern(new B { Name = "webabcd1", Age = 20 })); // aaaaaa Console.WriteLine(PositionalPattern(new B { Name = "webabcd2", Age = 30 })); // 000000 Console.WriteLine(PositionalPattern(new B { Name = "webabcd3", Age = 40 })); // bbbbbb } } }
4、演示默認接口方法
DefaultInterfaceMethods.cs
/* * C#8 新特性:默認接口方法 */ using System; namespace CSharp8 { // 接口可以有自己的默認方法了 interface IA { void M1(); void M2() { Console.WriteLine("IA.M2"); } void M3() => Console.WriteLine("IA.M3"); } // 類在繼承接口時,如果接口的某個方法有自己的默認方法,則類可以不去實現此方法 class B : IA { public void M1() { Console.WriteLine("B.M1"); } // 必須實現此方法 public void M2() { Console.WriteLine("B.M2"); } // 可以不實現此方法,如果實現了則覆蓋 // 可以不實現接口的 M3() 方法 } public class DefaultInterfaceMethods { public static void Demo() { B b = new B(); b.M1(); // B.M1 b.M2(); // B.M2 // 注意:對象 b 是沒有 M3() 方法的 // b1.M3(); // 如果要想調用對象 b 的 M3() 方法,則可以這么寫 IA a = b; a.M3(); // IA.M3 } } }
OK
[源碼下載]
