C# 9 中進一步加強了模式匹配的用法,使得模式匹配更爲強大,咱們一塊兒來了解一下吧git
C# 9 中加強了模式匹配的用法,增長了 and
/or
/not
操做符,並且能夠直接判斷屬性,來看一下下面的這個示例:github
var person = new Person(); // or // string.IsNullOrEmpty(person.Description) if (person.Description is null or { Length: 0 }) { Console.WriteLine($"{nameof(person.Description)} is IsNullOrEmpty"); } // and // !string.IsNullOrEmpty(person.Name) if (person.Name is not null and { Length: > 0 }) { if (person.Name[0] is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.') { } } // not if (person.Name is not null) { }
這裏的代碼使用 DnSpy
反編譯以後的代碼是下面這樣的:code
Person person = new Person(); string text = person.Description; bool flag = text == null || text.Length == 0; if (flag) { Console.WriteLine("Description is IsNullOrEmpty"); } text = person.Name; bool flag2 = text != null && text.Length > 0; if (flag2) { char c = person.Name[0]; if (c >= 'a') { if (c > 'z') { goto IL_8B; } } else if (c >= 'A') { if (c > 'Z') { goto IL_8B; } } else if (c != ',' && c != '.') { goto IL_8B; } bool flag3 = true; goto IL_8E; IL_8B: flag3 = false; IL_8E: bool flag4 = flag3; if (flag4) { } } bool flag5 = person.Name != null; if (flag5) { }
這不只適用於 is
也能夠在 switch
中使用ip
switch (person.Age) { case >= 0 and <= 3: Console.WriteLine("baby"); break; case > 3 and < 14: Console.WriteLine("child"); break; case > 14 and < 22: Console.WriteLine("youth"); break; case > 22 and < 60: Console.WriteLine("Adult"); break; case >= 60 and <= 500: Console.WriteLine("Old man"); break; case > 500: Console.WriteLine("monster"); break; }
反編譯後的代碼:get
int age = person.Age; int num = age; if (num < 22) { if (num < 14) { if (num >= 0) { if (num > 3) { Console.WriteLine("child"); } else { Console.WriteLine("baby"); } } } else if (num > 14) { Console.WriteLine("youth"); } } else if (num < 60) { if (num > 22) { Console.WriteLine("Adult"); } } else if (num > 500) { Console.WriteLine("monster"); } else { Console.WriteLine("Old man"); }
能夠看到有些狀況下能夠簡化很多代碼,尤爲是 if
分支比較多的狀況下使用上面 switch
這樣的寫法會清晰不少string
可是若是隻是 string.IsNullOrEmpty
這種代碼最好仍是不要寫得這麼騷了,當心要被同事吐槽了it
炫技需謹慎,當心被 ...io