c# 使用数组输入切换表达式用法

问题描述 投票:0回答:1

我想知道是否可以使用带有多个输入的 switch 表达式作为数组。我试过了,但没用,但也许我的语法有问题。这就是我所说的多重输入:

public static string EvaluteIt(int[] rawClusterValues)
    {
        string result = "Unknown";

        switch(rawClusterValues)
        {
            case [0, 9, 4, 4, 4, 5]:
                {
                    result = "some result here";
                    break;
                }

            case [ 1, 0, 0, 0, 0, 0 ]:
                {
                    result = "It is found";
                    break;
                }
            case [ 3, 2, 0, 0, 0, 1 ]:
                {
                    result = "this is another result";
                    break;
                }

            default:
                {
                    result = "result is default";
                    break;
                }

        }

        return result;
    }
c# .net switch-statement .net-8.0
1个回答
0
投票

Chat GPT 已经回答了我的问题。这是它的解决方案:

public static string EvaluateIt(int[] rawClusterValues)

{ 字符串结果=“未知”;

switch (rawClusterValues)
{
    case var arr when arr.SequenceEqual(new[] { 0, 9, 4, 4, 4, 5 }):
        result = "some result here";
        break;
    case var arr when arr.SequenceEqual(new[] { 1, 0, 0, 0, 0, 0 }):
        result = "It is found";
        break;
    case var arr when arr.SequenceEqual(new[] { 3, 2, 0, 0, 0, 1 }):
        result = "this is another result";
        break;
    default:
        result = "result is default";
        break;
}

return result;

}

© www.soinside.com 2019 - 2024. All rights reserved.