C# 9 新特性 —— 增强的模式匹配

Intro

C# 9 中进一步增强了模式匹配的用法,使得模式匹配更为强大,我们一起来了解一下吧

Sample

C# 9 中增强了模式匹配的用法,增加了 and/or/not 操作符,而且可以直接判断属性,来看一下下面的这个示例:

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

可以看到有些情况下可以简化不少代码,尤其是 if 分支比较多得情况下使用上面 switch 这样得写法会清晰很多

但是如果只是 string.IsNullOrEmpty 这种代码最好还是不要写得这么骚了,小心要被同事吐槽了

炫技需谨慎,小心被 diss ...

Reference

最新文章

  1. 【NLP】揭秘马尔可夫模型神秘面纱系列文章(四)
  2. js返回顶部效果
  3. js中cookie的使用详细分析
  4. centos7 安装nginx和php5.6.25遇到 无法访问php页面 报错file not found 问题解决
  5. matlab之round any size rat isscalar ismatrix mean find max
  6. fsck检查和修复文件系统
  7. 设计模式:职责链模式(Chain Of Responsibility)
  8. ruby 编写迭代器
  9. Android - TextView Ellipsize属性
  10. python 性能优化
  11. Vue中之nextTick函数源码分析
  12. [国嵌攻略][155][I2C用户态驱动设计]
  13. ABP官方文档翻译 3.1 实体
  14. 将Ext JS 5应用程序导入Web项目以及实现本地化
  15. HotSpot虚拟机
  16. web前端优化
  17. (转)Makefile介绍
  18. 【代码笔记】iOS-TableViewOfTwoSecton
  19. 20155338 2016-2017-2 《Java程序设计》第6周学习总结
  20. 从Web Service和Remoting Service引出WCF服务

热门文章

  1. Vue3教程:Vue 3.x 快在哪里?
  2. 第7.11节 案例详解:Python类实例变量
  3. PyQt(Python+Qt)学习随笔:QAbstractItemView的tabKeyNavigation属性
  4. Oracle命令管理账户和权限
  5. msfvenom命令自动补全工具下载=&gt;msfvenom-zsh-completion
  6. 《深入理解计算机系统》实验二 —— Bomb Lab
  7. IE各版本和Windows的关系
  8. STL——容器(List)list 的赋值操作
  9. DG修改SYS用户密码(ORA-16810,ORA-01017)
  10. python绘折线图