如何将C#模式匹配与元组一起使用

问题描述

我正在试验switch语句模式匹配,并且正在寻找一种方法,如果两个值元组中的任何一个值为零,则返回false。这是我正在尝试的代码

static bool IsAnyValueZero((decimal,decimal) aTuple)
{
    switch(aTuple)
    {
        case (decimal,decimal) t when t.Item1 == 0 || t.Item2 == 0:
            return true;
    }
    return false;
}

在VSCode 1.47和dotnetcore 3.14中,出现编译时错误

CS8652:功能“类型模式”在“预览”中

编写此代码的最佳兼容方式是什么?

解决方法

Type pattern中的

C# 8不支持与格式为(decimal,decimal) t的元组类型匹配。但是我们可以通过指定用于表示C#中元组的类型ValueTuple与元组类型进行匹配:

public static bool IsAnyValueZero((decimal,decimal) aTuple)
{
    switch (aTuple)
    {
        case ValueTuple<decimal,decimal> t when t.Item1 == 0 || t.Item2 == 0:
            return true;
    }
    return false;
}

这里是demo


另一种编写代码的方法是使用tuple pattern

public static bool IsAnyValueZero((decimal,decimal) aTuple)
{
    switch (aTuple)
    {
        case (decimal i1,decimal i2) when i1 == 0 || i2 == 0:
            return true;
    }
    return false;
}

或者我们可以通过以下方式重写此代码:

public static bool IsAnyValueZero((decimal,decimal) aTuple)
{
    switch (aTuple)
    {
        // Discards (underscores) are required in C# 8. In C# 9 we will
        // be able to write this case without discards.
        // See https://github.com/dotnet/csharplang/blob/master/proposals/csharp-9.0/patterns3.md#type-patterns.
        case (decimal _,decimal _) t when t.Item1 == 0 || t.Item2 == 0:
            return true;
    }
    return false;
}

我们还可以显式指定匹配值:

public static bool IsAnyValueZero((decimal,decimal) aTuple)
{
    switch (aTuple)
    {
        case (0,_):
            return true;
        case (_,0):
            return true;
    }
    return false;
}

这里是demo


C# 9type pattern进行了改进,以便我们可以使用下一种语法(如您的原始代码示例中一样)与元组类型进行匹配:

switch (aTuple)
{
    // In C# 9 discards (underscores) are not required.
    case (decimal,decimal) t when t.Item1 == 0 || t.Item2 == 0:
        return true;
}

此功能位于C# 9 preview中,可以为enabled