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 这种代码最好还是不要写得这么骚了,小心要被同事吐槽了
炫技需谨慎,小心被 diss ...
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# 7.0 新特性3: 模式匹配
本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...
- 使用 HTML5 History 新特性增强 Ajax 的体验(转)
一. 场景再现 如大家熟知,Ajax 可以实现页面的无刷新操作,但会造成两个与普通页面操作(有刷新地改变页面)有着明显差别的问题—— URL 没有修改以及无法使用前进.后退按钮.例如常见的 Ajax ...
- C# 9 新特性 —— 增强的 foreach
C# 9 新特性 -- 增强的 foreach Intro 在 C# 9 中增强了 foreach 的使用,使得一切对象都有 foreach 的可能 我们来看一段代码,这里我们试图遍历一个 int 类 ...
- jdk1.5出现的新特性---->增强for循环
import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; ...
- Java12新特性 -- 增强G1,自动返回未用堆内存给操作系统
Java 12 中增强了 G1 垃圾收集器关于混合收集集合的处理策略,这节主要介绍在 Java 12 中同时也对 G1垃圾回收器进行了改进,使其能够在空闲时自动将 Java 堆内存返还给操作系统,这也 ...
- Java JDK5新特性-增强for
2017-10-31 00:02:16 格式: for(元素数据类型 变量:数组或者Collection集合) { 使用变量即可,该变量即是元素 } int arr[] = {1,2,3,4,5}; ...
- Java程序员必备基础:JDK 5-15都有哪些经典新特性
前言 JDK 15发布啦~ 我们一起回顾JDK 5-15 的新特性吧,大家一起学习哈~ 本文已经收录到github ❝ https://github.com/whx123/JavaHome ❞ 「公众 ...
- C# 7.0 新特性2: 本地方法
本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C# 7.0 新特性1: 基于Tuple的“多”返回值方法
本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...
随机推荐
- 赶紧收藏吧!MyBatis-Plus万字长文图解笔记,错过了这个村可就没这个店了
简介 MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生 愿景 我们的愿景是成为 MyBatis 最好的搭档 ...
- moviepy音视频剪辑VideoClip类fl_image方法及参数image_func的功能介绍
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...
- 科大讯飞语音合成系统 V5.0绿色便携版
中文名: 中科大讯飞Interphonic 5.0语音合成系统英文名: Interphonic 5.0版本: 5.0发行时间: 2006年制作发行: 中科大讯飞语言: 简体中文系统简介InterPho ...
- 剑指offer二刷——数组专题——斐波那契数列
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1). n<=39 我的想法 斐波那契数列定义:F(0)=0,F(1)=1, ...
- v-lazyload数据变化图片不切换
这个问题让我很困惑,明明得到的商品数据已经改变了,但是就图片不变化,随后找到了解决办法,那就是多加一个动态的key <img v-lazy="item.productImage&quo ...
- 实现一个类型判断函数,需要鉴别出基本类型、function、null、NaN、数组、对象?
只需要鉴别这些类型那么使用typeof即可,要鉴别null先判断双等判断是否为null,之后使用typeof判断,如果是obejct的话,再用Array.isArray判断是否为数组,如果是数字再使用 ...
- Spring framework核心
这一部分涵盖了Spring框架绝对不可或缺的所有技术. 1.IOC容器 1.1Spring IoC容器和beans介绍 org.springframework.beans和org.springfram ...
- 【题解】三角形 [P1222] / 三角形覆盖问题 [HNOI2012] [P3219]
[题解]三角形 [P1222] / 三角形覆盖问题 [HNOI2012] [P3219] 传送门: 三角形 \(\text{[P1222]}\) 三角形覆盖问题 \(\text{[HNOI2012] ...
- 数据结构—— Trie (前缀树)
实现一个 Trie (前缀树),包含 插入, 查询, 和 查询前缀这三个操作. Trie trie = new Trie(); trie.insert("apple"); trie ...
- 零基础的Java小白如何准备初级开发的面试
对于各位Java程序员来说,只要能有实践的机会,哪怕工资再低,公司情况再一般,只要自己上心努力,就可能在短时间内快速提升,甚至在工作2年后进大厂都有希望,因为项目里真实的开发实践环境是平时学习不能模拟 ...