C# 9 新特性 —— 增强的模式匹配
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
这种代码最好还是不要写得这么骚了,小心要被同事吐槽了
炫技需谨慎,小心被 ...
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
C# 9 新特性 —— 增强的模式匹配相关推荐
- C# 9 新特性 —— 增强的 foreach
C# 9 新特性 -- 增强的 foreach Intro 在 C# 9 中增强了 foreach 的使用,使得一切对象都有 foreach 的可能 我们来看一段代码,这里我们试图遍历一个 int 类 ...
- C# 7.0 新特性3: 模式匹配
本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的"多"返回值方法 2. C# 7.0 新特性2: 本 ...
- Java春招面试复习:JDK 5-15都有哪些经典新特性
前言 JDK 15发布啦~ 我们一起回顾JDK 5-15 的新特性吧,大家一起学习哈~ 本文已经收录到github https://github.com/whx123/JavaHome 公众号:捡田螺 ...
- java 怎么匹配文章_Java 14 之模式匹配,非常赞的一个新特性!
今天栈长带大家来尝尝 Java14 的鲜,虽然大家都在用着 Java8 或者以下版本,但多学习了解一点总不是坏事. 比如出去面试啥的,面试官总会问你在学什么新技术,多学习下新知识可以出去和面试官吹水了 ...
- JDK8-JDK17中的新特性(var类型推断、模式匹配、Record、密封类)
文章目录 1. 新语法结构 1.1 Java的REPL工具: jShell命令 1.2 异常处理之try-catch资源关闭 1.3 局部变量类型推断 1.4 instanceof的模式匹配 1.5 ...
- Oracle 11g 新特性 -- Transparent Data Encryption (透明数据加密TDE) 增强 说明
一.TransparentData Encryption (TDE:透明数据加密) 说明 Orace TDE 是Orcle 10R2中的一个新特性,其可以用来加密数据文件里的数据,保护从操作系统层面上 ...
- Spring4新特性——Web开发的增强
2019独角兽企业重金招聘Python工程师标准>>> Spring4新特性--泛型限定式依赖注入 Spring4新特性--核心容器的其他改进 Spring4新特性--Web开发的增 ...
- Java SE 6 新特性: HTTP 增强--转
概述 Java 语言从诞生的那天起,就非常注重网络编程方面的应用.随着互联网应用的飞速发展,Java 的基础类库也不断地对网络相关的 API 进行加强和扩展.在 Java SE 6 当中,围绕着 HT ...
- Oracle12c功能增强 新特性之管理功能的增强
1. 数据文件在线重命名和迁移 不想先前的版本号.在Oracle12cR1中,数据文件的迁移或重命名不再要求一系列的步骤,比如:将表空间至于仅仅读模式,然后数据文件逻辑等操作.在12cR1中.数 ...
最新文章
- VS中的 MD/MT设置
- 国家电网和南方电网还傻傻分不清?
- 37. Sudoku Solver
- 数据结构-Hash总结(二)
- 蓝桥杯 ADV-169 算法提高 士兵排队问题
- scipy.sparse、pandas.sparse、sklearn稀疏矩阵的使用
- 数据可视化制作工具推荐
- 组合模式——公司组织结构
- BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)
- jquery 判断checkbox是否为空的三种方法
- 遗传算法在计算机视觉上的应用,遗传算法在视觉感知方面的应用
- php 汉字unico编码范围,汉字 Unicode 编码范围
- 图文配置docker阿里云加速器教程
- 实现图像批量重命名处理方法(mat文件、matlab简单程序等方法)
- Win10禁止Nvidia显卡驱动程序自动更新
- 《Python编程无师自通》第20章 融会贯通
- 2020哈工程计算机考研复试——网络线上自述
- 自己申请计算机软件著作权可以下载电子版原件了?
- linux切换用户时释放资源,linux 切换用户报Resource temporarily unavailable
- 我的内存怎么不够用了?
热门文章
- SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建
- Linux 2.6 完全公平调度算法CFS(Completely Fair Scheduler) 分析
- Win7系统中必需记住的14个常用快捷键
- java乘以2的位计算符号_java编程之:按位与运算,等运算规则
- 每天练习50个shell
- 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】
- APP测试流程和测试点
- Centos Missing Library: QtWebKit.so.4
- 在PowerDesigner中设计物理模型1——表和主外键
- express 4 简单实现自动注册路由功能