如何使用 C# 模式匹配元组

eod*_*uga 5 c# switch-statement c#-8.0

我正在试验 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;
}
Run Code Online (Sandbox Code Playgroud)

在 VSCode 1.47 和 dotnetcore 3.14 中,我收到一个编译时错误:

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

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

Ili*_*hev 6

Type patterninC# 8不支持匹配表单中的元组类型(decimal, decimal) t。但是我们可以通过指定ValueTuple用于表示 中的元组的类型来匹配元组类型C#

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;
}
Run Code Online (Sandbox Code Playgroud)

这是演示


另一种编写代码的方法是使用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;
}
Run Code Online (Sandbox Code Playgroud)

或者我们可以用下一种方式重写这段代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我们也可以明确指定匹配值:

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

这是演示


C# 9添加了改进,type pattern以便我们能够使用下一个语法(如您的原始代码示例中)匹配元组类型:

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

此功能已启用C# 9 preview并且可以启用