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

C# 9 新特性 —— 增强的模式匹配的更多相关文章

  1. C# 7.0 新特性3: 模式匹配

    本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...

  2. 使用 HTML5 History 新特性增强 Ajax 的体验(转)

    一. 场景再现 如大家熟知,Ajax 可以实现页面的无刷新操作,但会造成两个与普通页面操作(有刷新地改变页面)有着明显差别的问题—— URL 没有修改以及无法使用前进.后退按钮.例如常见的 Ajax ...

  3. C# 9 新特性 —— 增强的 foreach

    C# 9 新特性 -- 增强的 foreach Intro 在 C# 9 中增强了 foreach 的使用,使得一切对象都有 foreach 的可能 我们来看一段代码,这里我们试图遍历一个 int 类 ...

  4. jdk1.5出现的新特性---->增强for循环

    import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; ...

  5. Java12新特性 -- 增强G1,自动返回未用堆内存给操作系统

    Java 12 中增强了 G1 垃圾收集器关于混合收集集合的处理策略,这节主要介绍在 Java 12 中同时也对 G1垃圾回收器进行了改进,使其能够在空闲时自动将 Java 堆内存返还给操作系统,这也 ...

  6. Java JDK5新特性-增强for

    2017-10-31 00:02:16 格式: for(元素数据类型 变量:数组或者Collection集合) { 使用变量即可,该变量即是元素 } int arr[] = {1,2,3,4,5}; ...

  7. Java程序员必备基础:JDK 5-15都有哪些经典新特性

    前言 JDK 15发布啦~ 我们一起回顾JDK 5-15 的新特性吧,大家一起学习哈~ 本文已经收录到github ❝ https://github.com/whx123/JavaHome ❞ 「公众 ...

  8. C# 7.0 新特性2: 本地方法

    本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  9. C# 7.0 新特性1: 基于Tuple的“多”返回值方法

    本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...

随机推荐

  1. Docker实战 | 第三篇:Docker安装Nginx,实现基于vue-element-admin框架构建的项目线上部署

    一. 前言 在上一文中 点击跳转 通过IDEA集成Docker插件实现微服务的一键部署,但 youlai-mall 是前后端分离的项目,除了后端微服务的部署之外,当然还少不了前端工程的部署.所以本篇讲 ...

  2. 第15.43节、PyQt输入部件:QAbstractSpinBox派生类QSpinBox、 QDoubleSpinBox、QDateTimeEdit、QDateEdit和QTimeEdit功能简介

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer输入部件中的Spin B ...

  3. 第13.4 使用pip安装和卸载扩展模块

    一.pip指令介绍 Python 使用pip来管理扩展模块,包括安装和卸载,具体指令包括: pip install xx: 安装xx模块 pip list: 列出已安装的模块 pip install ...

  4. 第15.15节 PyQt(Python+Qt)入门学习:Designer的menu菜单、toolBar工具栏和Action动作详解

    老猿Python博文目录 老猿Python博客地址 一.引言 Qt Designer中的部件栏并没有菜单.toolBar以及Action相关的部件,仅在MainWindow类型窗口提供了menu.to ...

  5. 《Machine Learning in Action》—— Taoye给你讲讲Logistic回归是咋回事

    在手撕机器学习系列文章的上一篇,我们详细讲解了线性回归的问题,并且最后通过梯度下降算法拟合了一条直线,从而使得这条直线尽可能的切合数据样本集,已到达模型损失值最小的目的. 在本篇文章中,我们主要是手撕 ...

  6. luogu P6835 概率DP 期望

    luogu P6835 概率DP 期望 洛谷 P6835 原题链接 题意 n + 1个节点,第i个节点都有指向i + 1的一条单向路,现在给他们添加m条边,每条边都从一个节点指向小于等于自己的一个节点 ...

  7. AcWing 294. 计算重复

    暴力 其实这题的暴力就是个模拟.暴力扫一遍 \(conn(s_1, n_1)\),若出现了 \(res\) 个 \(s_2\). 答案就是 \(\lfloor res / n1 \rfloor\). ...

  8. Kubernetes Python Client 初体验之Deployment

    Kubernetes官方推荐我们使用各种Controller来管理Pod的生命周期,今天写一个最常用的Deployment的操作例子. 首先是创建Deployment: with open(path. ...

  9. swiper4使用教程-填坑

    本篇博客用于记录使用swiper插件中的一些关键点: 首先附上官网地址:https://www.swiper.com.cn/ ios中使用swiper的坑: /*解决swiper中苹果点击变暗,在cs ...

  10. 数据结构与算法——图(游戏中的自动寻路-A*算法)

    在复杂的 3D 游戏环境中如何能使非玩家控制角色准确实现自动寻路功能成为了 3D 游戏开 发技术中一大研究热点.其中 A*算法得到了大量的运用,A*算法较之传统的路径规划算法,实时性更高.灵活性更强, ...