<分区>
如何在一个case
中处理多个值?因此,如果我想对值 "first option"
和 "second option"
执行相同的操作?
这是正确的方法吗?
switch(text)
{
case "first option":
{
}
case "second option":
{
string a="first or Second";
break;
}
}
<分区>
如何在一个case
中处理多个值?因此,如果我想对值 "first option"
和 "second option"
执行相同的操作?
这是正确的方法吗?
switch(text)
{
case "first option":
{
}
case "second option":
{
string a="first or Second";
break;
}
}
最佳答案
它在文档中称为“多个标签”,可以在C# documentation 中找到。在 MSDN 上。
A switch statement can include any number of switch sections, and each section can have one or more case labels (as shown in the string case labels example below). However, no two case labels may contain the same constant value.
您修改后的代码:
string a = null;
switch(text)
{
case "first option":
case "second option":
{
a = "first or Second";
break;
}
}
请注意,我拉出了 string a
,否则你的 a
将只能在 switch
中使用。
关于c# - 如何在一个案例中处理多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943658/