C#9新特性之增強(qiáng)的模式匹配
Intro
C# 9 中進(jìn)一步增強(qiáng)了模式匹配的用法,使得模式匹配更為強(qiáng)大,我們一起來(lái)了解一下吧
Sample
C# 9 中增強(qiáng)了模式匹配的用法,增加了 and / or / not 操作符,而且可以直接判斷屬性,來(lái)看一下下面的這個(gè)示例:
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 反編譯之后的代碼是下面這樣的:
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)
{
}
Switch
這不僅適用于 is 也可以在 switch 中使用
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;
}
反編譯后的代碼:
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");
}
More
可以看到有些情況下可以簡(jiǎn)化不少代碼,尤其是 if 分支比較多的情況下使用上面 switch 這樣的寫(xiě)法會(huì)清晰很多
但是如果只是 string.IsNullOrEmpty 這種代碼最好還是不要寫(xiě)得這么騷了,小心要被同事吐槽了
炫技需謹(jǐn)慎,小心被 ...
Reference
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9
https://github.com/WeihanLi/SamplesInPractice/tree/master/CSharp9Sample
https://github.com/WeihanLi/SamplesInPractice/blob/master/CSharp9Sample/PatternMatchingSample.cs
到此這篇關(guān)于C#9新特性之增強(qiáng)的模式匹配的文章就介紹到這了,更多相關(guān)C#9 模式匹配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#簡(jiǎn)單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#簡(jiǎn)單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11
C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果
這篇文章主要介紹了C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果,涉及時(shí)間函數(shù)的應(yīng)用及繪圖的方法,需要的朋友可以參考下2014-10-10
C#使用自定義算法對(duì)數(shù)組進(jìn)行反轉(zhuǎn)操作的方法
這篇文章主要介紹了C#使用自定義算法對(duì)數(shù)組進(jìn)行反轉(zhuǎn)操作的方法,涉及C#針對(duì)數(shù)組操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行base64編碼的方法
這篇文章主要介紹了C#對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行base64編碼的方法,涉及C#中Convert.ToBase64String用法技巧,需要的朋友可以參考下2015-04-04
詳解ObjectARX開(kāi)發(fā)環(huán)境的創(chuàng)建與開(kāi)發(fā)實(shí)例Hello World(VS2005+AutoCad2008+Object
這篇文章主要介紹了ObjectARX開(kāi)發(fā)環(huán)境的創(chuàng)建與開(kāi)發(fā)實(shí)例Hello World(VS2005+AutoCad2008+ObjectArx2008),本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
winform用datagridview制作課程表實(shí)例
這篇文章主要介紹了winform用datagridview制作課程表的方法,實(shí)例分析了WinForm實(shí)現(xiàn)課程表的結(jié)構(gòu)、數(shù)據(jù)庫(kù)及調(diào)用技巧,需要的朋友可以參考下2015-01-01
C#使用偽隨機(jī)數(shù)實(shí)現(xiàn)加密用戶(hù)密碼的方法
這篇文章主要介紹了C#使用偽隨機(jī)數(shù)實(shí)現(xiàn)加密用戶(hù)密碼的方法,對(duì)于開(kāi)發(fā)C#會(huì)員系統(tǒng)或者程序安全問(wèn)題都有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-07-07

