Is empty case of switch in C# combined with the next non-empty one?(C# 中 switch 的空情况是否与下一个非空情况相结合?)
问题描述
使用以下代码:
case "GETSITES":
case "SITESETUP":
MessageBox.Show("Help! I am suffering from the Air-Conditioned Nightmare!!!");
// ...
MessageBox.Show
是否会被执行,开关值是 "GETSITES"
还是 "SITESETUP"
?
Will the MessageBox.Show
be executed either the switch value is "GETSITES"
or "SITESETUP"
?
或仅如果开关值为SITESETUP"
?
由于 "GETSITES"
没有中断,我想是的,但不确定.
Since "GETSITES"
has no break, I'm thinking yes, but am not sure.
更新
我想我应该这样表述我的问题:
I guess the way I should have worded my question as:
这两个代码片段在语义上是等价的吗?
Are these two code fragments semantically equivalent?
片段 1
fragment 1
case 0:
case 1:
// bla bla bla;
break;
片段2(伪代码)
fragment 2(pseudo code)
case 0, 1:
// bla bla bla;
break;
推荐答案
您所描述的是同一案例有两个标签.C# 确实允许这样做.但是,您不能以 C 允许的相同方式进入下一个 case 语句,也就是说,您的标签不能有一些代码,然后进入下一个 case 语句.这是禁止的,会导致编译错误.
What you are describing is having two labels for the same case. C# does allow this. You cannot, however, fall through to the next case statement in the same way that C allows, that is, your label cannot have some code, then fall through to the next case statement. That is forbidden and will cause a compilation error.
这篇关于C# 中 switch 的空情况是否与下一个非空情况相结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!