如果 C# 不是可选的,为什么还要有break?
当我在 VS2008 C# 中创建一个 switch
语句时,如下所示(人为的):
switch (state) {
case '1':
state = '2';
case '2':
state = '1';
}
它抱怨我不允许删除:
控制不能从一个 case 标签('case '1' (0x31):')转移到另一个 case 标签
如果不允许落入,那么 break
语句的目的到底是什么?为什么语言设计者不直接忽略它并自动跳转到 switch 语句的末尾,而不是强迫我们放入不必要的构造?
When I create a switch
statement in VS2008 C# like this (contrived):
switch (state) {
case '1':
state = '2';
case '2':
state = '1';
}
it complains that I'm not allowed to drop through:
Control cannot fall through from one case label ('case '1' (0x31):') to another
If you're not allowed to drop through, then what is the purpose of the break
statement at all? Why didn't the language designers just leave it out and automatically jump to the end of the switch
statement instead of forcing us to put in an unnecessary construct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上是为了让 C/C++/Java 开发人员更熟悉它。我个人认为这是一个错误,但这就是原因。
我更喜欢强制块:
除了其他任何事情之外,这可以避免 switch/case 的奇怪的变量范围情况。当然,您仍然可以有多个案例标签:
能够更简单地列出多个案例也可能会很好:
哦,对现有语言的描述有一点挑剔:您没有 休息一下。只是案件的结局注定是遥不可及的。您还可以使用
throw
、goto
或return
。可能还有其他我也错过了:)Basically to make it more familiar to C/C++/Java developers. Personally I think it was a mistake, but that's the reasoning.
I would have preferred a forced block:
Aside from anything else, that would have avoided the weird variable scoping situations for switch/case. You could still have multiple case labels, of course:
It might also be nice to be able to list multiple cases more simply:
Oh, and a slight nit-pick about your description of the existing language: you don't have to have a break. It's just that the end of the case has to be unreachable. You can also have
throw
,goto
orreturn
. There may be others that I've missed, too :)来自马口(MSDN)为什么 C# switch 语句被设计为不允许失败,但仍然需要中断?。
引用显着的位,这就是为什么他们不允许失败:
这就是为什么它不是自动的:
From the horse's mouth (MSDN) Why is the C# switch statement designed to not allow fall-through, but still require a break?.
Quoting the salient bits, this is why they don't allow fall-through:
And this is why it's not automatic:
您可以直接访问,但必须使用
goto
关键字显式这样做:您可以
break
或goto 在 C# 中,但你不能做的是不声明你想要哪个,因为这是难以发现错误的潜在来源。
当您想要
break
(或反之亦然)时,发现您的代码说goto
比发现您忘记添加要容易得多。这听起来可能很愚蠢,但许多人在花费两个小时寻找 C++ bug 原因的疲惫搜索后,最终会突然意识到您忘记添加
break
并且您的代码一直在失败。 C# 通过强制您声明您想要的内容来避免这种情况。You are allowed to drop through, but you have to do so explicitly with the
goto
keyword:You can
break
orgoto
in C#, but what you cannot do is not state which you want, because that's a potential source of hard-to-spot bugs.It's a lot easier to spot that your code says
goto
when you wantedbreak
(or vice versa) than it is to spot that you forgot to add either.It might sound stupid, but many a tired two-hour search for the cause of a C++ bug ends in a sudden realisation that you forgot to add a
break
and your code is falling through all the time. C# avoids that by forcing you to state what you want.如果你没有案例1中的任何代码,那么你就可以失败,所以你可以说“所有这些案例都共享这段代码”
If you don't have any code in case 1, you are allowed to fall through, so you can say that "all of these cases share this piece of code"