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

本文参考Roslyn项目Issue:#206,及Docs:#patterns

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

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

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

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

 

模式匹配也许能算的上C#本次更新最重量级的升级,也是最受关注的特性(也许没有之一),通过模式匹配,我们可以简化大量的条件代码。

 

Switch语句

大家也许遇到过这样的情景,假设你的代码中,有一个Nullable<int>的值,需要对其在正整数非正整数Null三种情况下分别作不同的逻辑处理。大多数童鞋直接想到是类似于下面的逻辑:

1 void Foo(int? num)
2 {
3     if (!num.HasValue)
4      /* null logic */
5     else if (num.Value > 0)
6      /* positive int logic */
7     else
8      /* negative int & zero logic */
9 }
View Code

请大家思考一下,这个逻辑是否可以用switch-case语句来做,在VB及很多非C系的语言中,答案是肯定的,比如VB.NET中可以这样写:

 1 Sub Foo(Num As Integer?)
 2     Select Case Num
 3         Case Not Num.HasValue
 4         'null logic
 5         Case Num > 0
 6         'positive Int logic
 7         Case Num <= 0
 8             'negative Int() & zero logic
 9         Case Else
10 
11     End Select
12 End Sub
View Code

说到这里,在具体讨论模式匹配在switch-case中的应用之前,先淡淡的吐槽一下C#,本来理所应当的一个简单的小语法,到了C#7.0才加入。

看看C#7.0加入的类型模式(Type Pattern):

 

 1 void Foo(int? num)
 2 {
 3     switch (num)
 4     {
 5         case null:
 6             //null logic
 7             break;
 8         case int n when n > 0:
 9             //positive Int logic
10             break;
11         case int n when n <= 0:
12             //negative Int() & zero logic
13             break;
14     }
15 }

 

这个不多说了,大家自己体会,单纯的在Nullable<int>下,可能体现的不是很清晰,个人认为这个小变动其实意义并不是很大,同样场景下,或许if-if else-else会让代码更清晰易读些。

 

如果说模式匹配仅仅是完善了一下switch-case,那可真是太大才小用了,下面我们看一个好玩的。 

Match表达式

虽然把match带到C#中看起来并不是什么大事,但是会引起的代码简化还是非常爽的。

就像很多人说三元表达式(<condition>?<true result> : <false result> )将if-else简化一样。match表达式,是将switch-case结构简化到了一个新限度。

看match表达式代码前,我们先来看一行略坑的三元表达式。

var reuslt = x == null ? default(int) : (x is Func<int> ? (x as Func<int>)() : (x is int ? Convert.ToInt32(x) : default(int)));

好吧,我承认我是故意让你们抓狂的。^_^, 为了能稳住大家看完上面这行代码后的情绪,来一副match表达式消消火。

var result = x match(
    case Func<int> f: f(),
    case int i: i,
    case *: default(int)
);

这两种写法效果上是等效的,有没有非常干净清爽的感觉?写过match表达式的码农,应该再也不想回去嵌套 <*>?<*>:<*> 了。 (注:目前这种写法还未确认,C#7.0发布后可能会有略微变动

 

Is表达式

如果说上面两个变化是“语法糖”,那么is表达式可是要玩真的了。

说点题外话,其实对正则表达式熟悉的童鞋可能知道,本质上[模式匹配]和正则表达式要解决的问题逻辑类似,以一个确定的模式,来判断或查找一个确定的实例。只不过在正则表达式中,这里说的"模式"是正则表达式,"实例"指字符串。而[模式匹配]下,所针对的"实例"是对象,那么"模式",就可以理解成is表达式了。

举个例子,比如你要查找并列出 一组电子设备中,所有iPhone的IMEI串号,我们在C#6.0中,会这样做:

 1 class Device
 2 {
 3     public ProductLineOption ProductLine { get; set; }
 4 }
 5 
 6 class MobiePhone : Device
 7 {
 8     public string IMEICode { get; set; }
 9 }
10 
11 IEnumerable<Device> GetAllDevices() { /* 获取并返回所有设备 */ };
12 
13 IEnumerable<string> GetAlliPhoneIMEI()
14 {
15     var deviceList = this.GetAllDevices();
16     foreach (Device device in deviceList)
17     {
18         MobiePhone phone = device as MobiePhone;
19         if (phone == null) continue;
20 
21         if (phone.ProductLine == ProductLineOption.IPhone)
22         {
23             yield return phone.IMEICode;
24         }
25     }
26 }

一个非常典型的传统方法,没什么好说的。我们直接来看C#7.0 中 is表达式怎么等效的实现这段逻辑:

 1 IEnumerable<string> GetAlliPhoneIMEI()
 2 {
 3     List<Device> deviceList = this.GetAllDevices();
 4     foreach (Device device in deviceList)
 5     {
 6         if (device is MobiePhone { IMEICode is var imei, ProductLine is ProductLineOption.IPhone})
 7         {
 8             yield return imei;
 9         }
10     }
11 }

如果你还是觉得这没什么,那么,其实这个例子中,仅仅体现出模式匹配中的属性模式

根据Doc:#patterns C#7.0会提供一下几种匹配方式:

  • 类型模式
  • 常量模式
  • 变量模式
  • 通配符模式
  • 位置模式
  • 属性模式

我们可以想象,如果模式匹配组合起来使用,会给现有的C#代码带来多大的便利和清静。

Okay,说了这么多,下面给大家一个相对完整的案例,自行体会。

 

案例 

 1 abstract class Animal
 2 {
 3     public string Name { get; set; }
 4 }
 5 
 6 class Dog : Animal
 7 {
 8     public string BarkLikeCrazy() => "WOOF WOOF WOOF";
 9 }
10 
11 class Cat : Animal { }
12 class Swan : Animal { }
13 
14 class Program
15 {
16     static void Main(string[] args)
17     {
18         var animals = new Animal[] {
19             new Dog { Name = "hola" },
20             new Cat { Name = "tom" },
21             new Swan { Name = "hacienda" }
22         };
23 
24         var organizedAnimals = from animal in animals
25                                let sound = animal match( //Match语句
26                                    case Dog d: "woof... " + d.BarkLikeCrazy(), //类型匹配
27                                    case Cat c: "meow",
28                                    case * : "I'm mute.." //通配符匹配
29                                )
30                                select new { Type = animal, Sound = sound };
31 
32         foreach (var animal in organizedAnimals)
33         {
34             Console.WriteLine($"{animal.Type.ToString()} - {animal.Sound}");
35         }
36 
37         foreach (var a in animals)
38         {
39             if (a is Cat { Name is var name }) //类型及属性匹配,is表达式
40             {
41                 Console.WriteLine($"Name of {nameof(Cat)} is {name}");
42             }
43 
44             string sound = "";
45             switch (a) //匹配switch语句
46             {
47                 case Dog d when d.Name == "hola":
48                     sound = "woof... hola" + d.BarkLikeCrazy();
49                     break;
50                 case Dog d:
51                     sound = "woof..." + d.BarkLikeCrazy();
52                     break;
53                 case Cat c:
54                     sound = "meow";
55                     break;
56                 case IEnumerable<Animal> l when l.Any():
57                     //TODO: any logic;
58                     break;
59                 case null:
60                     sound = "no animal";
61                     break;
62                 default:
63                     sound = "I'm mute..";
64                     break;
65             }
66             Console.WriteLine($"{a.ToString()} - {sound}");
67         }
68     }
69 }

  

注1:模式匹配的部分高级feature,已经确认在C#7.0中移除,可能出现在后续C#版本中。(#10888)。

注2:目前(2016-06-15VS15的最新Preview下,模式匹配的部分语法依然无法使用。

注3:由于目前仍然未在Roslyn中Release,后期有变动的可能,本文中涉及的样例代码以Mads Torgersen在#Build 2016上的演示的语法为准,本文涉及的案例有可能无法在VS15 RTM后正常使用,仅供参考。

  (当然,如果笔者乐意,会及时把后期得到确认的变更更新到本文中 ^_^!)

 

本文链接:http://www.cnblogs.com/ylvict/p/5588613.html (转载请注明)

目前(2016年6月)C#7.0还未正式发布,大家如果想体验部分特性,可以去下载VS15预览版,最终发布的语法可能和本文中提及的有所不同,最新动态请大家关注Roslyn项目。

posted @ 2016-06-15 19:04  Hover Zuo  阅读(10958)  评论(17编辑  收藏  举报