C# 开关/中断
看来我需要使用 C# 在 switch 语句中的每个 case 块中使用中断。
我可以在其他语言中看到这样做的原因,您可以在其中转到下一个案例陈述。
案例块是否有可能落入其他案例块?
非常感谢,非常感谢!
It appears I need to use a break in each case block in my switch statement using C#.
I can see the reason for this in other languages where you can fall through to the next case statement.
Is it possible for case blocks to fall through to other case blocks?
Thanks very much, really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以通过两种方式进入下一个案例块。您可以使用不需要中断的空案例,也可以使用
goto
跳转到下一个(或任何)案例:Yes, you can fall through to the next case block in two ways. You can use empty cases, which don't need a break, or you can use
goto
to jump to the next (or any) case:执行“break”是为了阻止错误。如果您需要强制失败,请使用“goto case”(将其替换为适当的值),
以下示例显示了您可以执行的操作:
请参阅 http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx
The enforcement of "break" is there to stop bugs. If you need to force a fall-thru then use "goto case " (replace the with appropriate value)
the following example shows what you can do:
See http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx
C# 不支持隐式失败构造,但
break
(或goto
)仍然必须存在(msdn)。您唯一能做的就是按以下方式堆栈案例:但是
break
(或另一个跳转语句,如goto
)只需要在那里。C# doesn't support implicit fall through construct, but the
break
(orgoto
) nonetheless has to be there (msdn). The only thing you can do is stack cases in the following manner:but that
break
(or another jump statement likegoto
) just needs to be there.在我的 C#(.NET 1.1、CF)代码中,这两种情况都是允许的:
...但是随着中断的出现,它们会变灰,因此被认为没有实际意义。因此,至少就我而言,它们是允许的,但不是必需的。
In my C# (.NET 1.1, CF) code, both of these are allowed:
...but with the breaks in, they are grayed out, so considered moot. So, at least in my case, they are allowed but not required.