【发布时间】:2018-06-21 09:06:57
【问题描述】:
在 C# 中是否可以在不中断的情况下执行 switch case?
这是一个需要使用中断的开关示例
var bar = "Foo";
switch (foo) {
case 0:
case 1:
bar += " Bar";
case 2:
case 3:
Console.WriteLine(bar);
break;
default:
break;
}
这是代码应该产生的:
0: Foo Bar
1: Foo Bar
2: Bar
3: Bar
Else: nothing
是否可以这样做或者我必须这样做:
var bar = "Foo";
if(foo == 0 || foo == 1) bar += " Bar";
switch (foo) {
case 0:
case 1:
case 2:
case 3:
Console.WriteLine(bar);
break;
default:
break;
}
【问题讨论】:
标签: c# switch-statement