C# Pattern Matching (is vs switch)

C#中可以通过 is 或者 switch 匹配各种各样的Pattern。 

Pattern主要类型: 

  1. Declaration pattern: 运行时的表达式类型,并给一个Local Variable赋值
  2. Type pattern:也是运行时表达式类型 
  3. Constant pattern: 监测表达式结果是否等于一个常量
  4. Relational pattern: 监测表达式结果是否大于or小于一个常量
  5. Logical pattern: 监测表达式是否满足一系列的逻辑判断
  6. Property pattern:监测表达式的字段或者属性是否满足 
  7. Position pattern: 析构表达式结果并做判断
  8. Var pattern: 匹配任意表达式并将结果赋给一个已经声明的变量
  9. Discard pattern: 用来匹配其他的情况,全收
  10. List patterns:用来判断一个系列的元素是否满足 

主要使用的方式: 

  1. is expression 
  2. switch statement/switch expresssion

is 没有switch使用方式灵活, 用is写得我目前觉得应该都能用switch替换掉,is主要用在以下场合: 

  1. 检查运行时的表达式类型,当declaration type为T时    
    1. run-time pattern 也为T 
      object greeting = "Hello, World!";
      if (greeting is string message)
      {
          Console.WriteLine(message.ToLower());  // output: hello, world!
      }
    2. run-timie pattern 从T中继承,是接口T的实现, 或者其他隐式引用类型转化,举例 
      var numbers = new int[] { 10, 20, 30 };
      Console.WriteLine(GetSourceLabel(numbers));  // output: 1
      
      var letters = new List<char> { 'a', 'b', 'c', 'd' };
      Console.WriteLine(GetSourceLabel(letters));  // output: 2
      
      static int GetSourceLabel<T>(IEnumerable<T> source) => source switch
      {
          Array array => 1,
          ICollection<T> collection => 2,
          _ => 3,
      };

      output: 

    3. run-time type 是nullable T

    4. 可以通过封箱或者拆箱进行转化
  2. 检查null 
  3. 从C# 9.0 检查 non-null
  4. 检查是否满足list pattern

is的主要使用方式举例: 

int? xNullable = 7;
int y = 23;
object yBoxed = y;
if (xNullable is int a && yBoxed is int b)
{
    Console.WriteLine(a + b);  // output: 30
}

分别对Xnullable和yBoxed进行判断,结果为真之后赋值给Local Variable a和b。 

switch在整体性上更胜一筹,举例来说,我有一个多种不同车辆的收费app,pattern match的优势在于这种不是从同一个base class继承而来的多个体系,判断会很方便: 

车辆类: 

namespace ConsumerVehicleRegistration
{
    public class Car
    {
        public int Passengers { get; set; }
    }
}

namespace CommercialRegistration
{
    public class DeliveryTruck
    {
        public int GrossWeightClass { get; set; }
    }
}

namespace LiveryRegistration
{
    public class Taxi
    {
        public int Fares { get; set; }
    }

    public class Bus
    {
        public int Capacity { get; set; }
        public int Riders { get; set; }
    }
}

Caculator 类

namespace Calculators;

public class TollCalculator
{
    public decimal CalculateToll(object vehicle) =>
        vehicle switch
        {
            Car c => 2.00m,
            Taxi t => 3.50m,
            Bus b => 5.00m,
            DeliveryTruck t => 10.00m,
            { } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
            null => throw new ArgumentNullException(nameof(vehicle))
        };
}

这里也初始化了本地变量 c t b 但是其实么有用到,所以也可以省略掉,写为: 

namespace Calculators;

public class TollCalculator
{
    public decimal CalculateToll(object vehicle) =>
        vehicle switch
        {
            Car => 2.00m,
            Taxi => 3.50m,
            Bus => 5.00m,
            DeliveryTruck t => 10.00m,
            { } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
            null => throw new ArgumentNullException(nameof(vehicle))
        };
}

可以加上vechicle字段或者属性作为判断条件的一部分,比如我假设Car类在乘客大于5的时候才会收费: Car {Passengers:>=5} c => 2.00m,

namespace Calculators;

public class TollCalculator
{
    public decimal CalculateToll(object vehicle) =>
        vehicle switch
        {
            Car {Passengers:>=5} c => 2.00m,
            Taxi t => 3.50m,
            Bus b => 5.00m,
            DeliveryTruck t => 10.00m,
            { } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
            null => throw new ArgumentNullException(nameof(vehicle))
        };
}

另外注意是switch的结果是可以作为右侧表达式的,举例来说: 

var shape = GetShape();

var area = shape switch
{
    Circle c => Math.PI * c.Radius * c.Radius,
    Rectangle r => r.Width * r.Height,
    _ => throw new ArgumentException("Unknown shape")
};

总结来说,switch相对于is的优势:

  1. switch是个single struct,整体性更好
  2. switch可以更好地覆盖所有的情况,为特殊情况提供解决方式
  3. switch可以在属性等一些更复杂的pattern matching情况进行使用
  4. switch可以作为expression的一部分
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值