C# 7.0 新特性:模式匹配 ( pattern matching )

在 C# 中,is 是一个关键字,可以用来检查某个数据的类型是否为特定类型。这是一个表达式,返回类型为 boolean。

例如,我们可以检查某个实例是否为 Persion 类型

if (obj is Person) {
// Do something if obj is a Person.
}

在下面情况下,返回 true:

  • 表达式的类型与 is 类型相符
  • 表达式的类型为 is 类型的派生类型
  • 表达式具有一个编译时类型, 它是 is 类型的基类,在运行时的值为 is 类型的派生类型
  • 表达式实现了 is 类型的接口

支持模式匹配的 is

类型模式

在 C# 7.0 中,is 在原来的基础上,额外提供了类型转换的支持。可以在类型检查的基础上,直接支持类型转换。

在下面的示例中,我们希望检查对象的类型,如果为指定类型,我们要转型为特定的类型进行操作。

using System;

public class Example
{
public static void Main()
{
Object o = new Person("Jane");
ShowValue(o); o = new Dog("Alaskan Malamute");
ShowValue(o);
} public static void ShowValue(object o)
{
if (o is Person p) {
Console.WriteLine(p.Name);
}
else if (o is Dog d) {
Console.WriteLine(d.Breed);
}
}
} public struct Person
{
public string Name { get; set; } public Person(string name) : this()
{
Name = name;
}
} public struct Dog
{
public string Breed { get; set; } public Dog(string breedName) : this()
{
Breed = breedName;
}
}
// The example displays the following output:
// Jane
// Alaskan Malamute

等价的以前代码为:

if (o is Person) {
Person p = (Person) o;
Console.WriteLine(p.Name);
}
else if (o is Dog) {
Dog d = (Dog) o;
Console.WriteLine(d.Breed);
}

常量匹配

is 后面还可以是常量,is 测试表达式的值是否为特定常量。在以前版本中,这需要使用 switch 来支持。

using System;

public class Dice
{
Random rnd = new Random();
public Dice()
{ }
public int Roll()
{
return rnd.Next(1, 7);
}
} class Program
{
static void Main(string[] args)
{
var d1 = new Dice();
ShowValue(d1);
} private static void ShowValue(object o)
{
const int HIGH_ROLL = 6; if (o is Dice d && d.Roll() is HIGH_ROLL)
Console.WriteLine($"The value is {HIGH_ROLL}!");
else
Console.WriteLine($"The dice roll is not a {HIGH_ROLL}!");
}
}
// The example displays output like the following:
// The value is 6!

var 匹配

如果 is 后面是 var,则永远为 true 。并把值赋予后面的变量。

例如,下面代码将 item 赋予了 obj。

if (item is var obj)

需要注意的是,即使被测试的值为 null,is 表达式还是为 true。此时,变量将被赋予 null。

支持模式匹配的 Switch

所有的 c# 版本都支持常量模式,在 C#  7.0 中,现在支持类型模式了。也就是说,在 case 后面还可以是一个用来检测的类型。

case type varname

相当于在这里使用了 is 。

从 C# 7.0 开始,您还可以在上面的表达式后面附加一个返回 boolean 的 when 条件,以进一步检查。使用它的常见场景就是当值为 null 时。

using System;

public abstract class Shape
{
public abstract double Area { get; }
public abstract double Circumference { get; }
} public class Rectangle : Shape
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
} public double Length { get; set; }
public double Width { get; set; } public override double Area
{
get { return Math.Round(Length * Width,2); }
} public override double Circumference
{
get { return (Length + Width) * 2; }
}
} public class Square : Rectangle
{
public Square(double side) : base(side, side)
{
Side = side;
} public double Side { get; set; }
} public class Circle : Shape
{
public Circle(double radius)
{
Radius = radius;
} public double Radius { get; set; } public override double Circumference
{
get { return 2 * Math.PI * Radius; }
} public override double Area
{
get { return Math.PI * Math.Pow(Radius, 2); }
}
} public class Example
{
public static void Main()
{
Shape sh = null;
Shape[] shapes = { new Square(10), new Rectangle(5, 7),
sh, new Square(0), new Rectangle(8, 8),
new Circle(3) };
foreach (var shape in shapes)
ShowShapeInfo(shape);
} private static void ShowShapeInfo(Shape sh)
{
switch (sh)
{
// Note that this code never evaluates to true.
case Shape shape when shape == null:
Console.WriteLine($"An uninitialized shape (shape == null)");
break;
case null:
Console.WriteLine($"An uninitialized shape");
break;
case Shape shape when sh.Area == 0:
Console.WriteLine($"The shape: {sh.GetType().Name} with no dimensions");
break;
case Square sq when sh.Area > 0:
Console.WriteLine("Information about square:");
Console.WriteLine($" Length of a side: {sq.Side}");
Console.WriteLine($" Area: {sq.Area}");
break;
case Rectangle r when r.Length == r.Width && r.Area > 0:
Console.WriteLine("Information about square rectangle:");
Console.WriteLine($" Length of a side: {r.Length}");
Console.WriteLine($" Area: {r.Area}");
break;
case Rectangle r when sh.Area > 0:
Console.WriteLine("Information about rectangle:");
Console.WriteLine($" Dimensions: {r.Length} x {r.Width}");
Console.WriteLine($" Area: {r.Area}");
break;
case Shape shape when sh != null:
Console.WriteLine($"A {sh.GetType().Name} shape");
break;
default:
Console.WriteLine($"The {nameof(sh)} variable does not represent a Shape.");
break;
}
}
}
// The example displays the following output:
// Information about square:
// Length of a side: 10
// Area: 100
// Information about rectangle:
// Dimensions: 5 x 7
// Area: 35
// An uninitialized shape
// The shape: Square with no dimensions
// Information about square rectangle:
// Length of a side: 8
// Area: 64
// A Circle shape

其它资源:

C# 7.0 新特性:模式匹配 ( pattern matching)的更多相关文章

  1. C# 9.0 新特性之模式匹配简化

    阅读本文大概需要 2 分钟. 记得在 MS Build 2020 大会上,C# 语言开发项目经理 Mads Torgersen 宣称 C# 9.0 将会随着 .NET 5 在今年 11 月份正式发布. ...

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

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

  3. C#6.0,C#7.0新特性

    C#6.0新特性 Auto-Property enhancements(自动属性增强) Read-only auto-properties (真正的只读属性) Auto-Property Initia ...

  4. 背水一战 Windows 10 (43) - C# 7.0 新特性

    [源码下载] 背水一战 Windows 10 (43) - C# 7.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 7.0 新特性 介绍 C# 7.0 的新特性 示例 ...

  5. [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐

    [翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...

  6. C# 9.0新特性

    CandidateFeaturesForCSharp9 看到标题,是不是认为我把标题写错了?是的,C# 8.0还未正式发布,在官网它的最新版本还是Preview 5,通往C#9的漫长道路却已经开始.前 ...

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

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

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

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

  9. C# 7.0 新特性4: 返回引用

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

  10. ASP.NET Web API 2.0新特性:Attribute Routing1

    ASP.NET Web API 2.0新特性:Attribute Routing[上篇] 对于一个针对ASP.NET Web API的调用请求来说,请求的URL和对应的HTTP方法的组合最终决定了目标 ...

随机推荐

  1. manim边学边做--形状匹配

    manim中有几个特殊的用于形状匹配的对象,它们的作用是标记和注释已有的对象,本身一般不单独使用. 形状匹配对象一共有4种: BackgroundRectangle:为已有的对象提供一个矩形的背景 C ...

  2. mmap映射类型

    文件映射和匿名映射都是操作系统中对于内存映射的两种类型,主要应用于进程间的通信或者大量数据的处理. 文件映射,也就是内存映射文件,是把一个文件或者文件的一部分映射到进程的地址空间,它允许对文件进行随机 ...

  3. OCR+PDF解析配套前端工具开源详解!

    面对日常生活和工作中常见的OCR识别.PDF解析.翻译.校对等场景,配套的可视化工具能够极大地提升我们的使用体验和工作效率. 通过可视化界面,我们可以直观地看到文本识别.解析和翻译的结果,便捷评估产品 ...

  4. 数据库周刊59丨GaussDB(for openGauss)开放商用;人大金仓保障冬奥会演练顺利完成;MDL锁导致的MySQL问题分析;PG日志使用手册;达梦表空间查询;数据库笔试题面试题集……

    热门资讯 1.openGauss 上线华为云正式商用 - GaussDB(for openGauss) [摘要]近日,GaussDB(for openGauss)已于华为云官网全面开放商用.该产品是华 ...

  5. 简述 JavaScript脚本的执行原理?

    js 是一种动态 . 弱类型 . 基于原型的语言 ,通过浏览器可以直接执行: 当浏览器遇到 <script></script>标记时 , 会执行标记之间的js 代码:然后js ...

  6. 神经网络之卷积篇:详解经典网络(Classic networks)

    详解经典网络 首先看看LeNet-5的网络结构,假设有一张32×32×1的图片,LeNet-5可以识别图中的手写数字,比如像这样手写数字7.LeNet-5是针对灰度图片训练的,所以图片的大小只有32× ...

  7. URL是什么

    URL是什么 URL(Uniform Resource Locator,统一资源定位器) URL的组成: 协议://{域名|主机名|IP}:端口/路径/文件名?参数#锚点 协议 Scheme/Prot ...

  8. 【241027-论文阅读】DGraph: A Large-Scale Financial Dataset for Graph Anomaly Detection

    DGraph: A Large-Scale Financial Dataset for Graph Anomaly Detection DGragh是一个用于图异常检测(gragh anomaly d ...

  9. 基于Material Design风格开源、免费的WinForms UI控件库

    前言 今天大姚给大家分享一个基于 Google 的 Material Design 风格开源.免费的.NET WinForms UI控件库:MaterialSkin. WinForms介绍 WinFo ...

  10. python 爬虫如何爬取动态生成的网页内容

    ---  好的方法很多,我们先掌握一种  --- [背景] 对于网页信息的采集,静态页面我们通常都可以通过python的request.get()库就能获取到整个页面的信息. 但是对于动态生成的网页信 ...