我可以使用包含多个案例标准的Switch语句吗?

MrM*_*MrM 2 .net c# asp.net

我可以使用这样的switch语句:

...

switch (temp)
{
case "NW" or "New":
temp = "new stuff"
break;
}
Run Code Online (Sandbox Code Playgroud)

...

lag*_*neb 15

不,但你可以使用(至少在Java中)

switch (temp) {
    case "NW":
    case "New":
       temp="new stuff";
       break;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果在特定情况下没有任何内容,C#确实允许在这一特殊情况下失败.所以上面是有效的C# (4认同)

Jos*_*lio 10

是.这就是它的完成方式.

switch (temp)
{
   case "NW":
   case "New":
     temp = "new stuff"
     break;
}
Run Code Online (Sandbox Code Playgroud)

实际上,我之前回答了同样的问题.