我想知道是否可以使用带有多个输入的 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;
}
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;
}