如果 C# 不是可选的,为什么还要有break?

发布于 2024-09-07 08:35:38 字数 382 浏览 6 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只有影子陪我不离不弃 2024-09-14 08:35:38

基本上是为了让 C/C++/Java 开发人员更熟悉它。我个人认为这是一个错误,但这就是原因。

我更喜欢强制块:

case '1':
{
}

除了其他任何事情之外,这可以避免 switch/case 的奇怪的变量范围情况。当然,您仍然可以有多个案例标签:

case '0':
case '1':
{
}

能够更简单地列出多个案例也可能会很好:

case '0', '1':
{
}

哦,对现有语言的描述有一点挑剔:您没有 休息一下。只是案件的结局注定是遥不可及的。您还可以使用 throwgotoreturn。可能还有其他我也错过了:)

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:

case '1':
{
}

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:

case '0':
case '1':
{
}

It might also be nice to be able to list multiple cases more simply:

case '0', '1':
{
}

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 or return. There may be others that I've missed, too :)

樱娆 2024-09-14 08:35:38

来自马口(MSDN)为什么 C# switch 语句被设计为不允许失败,但仍然需要中断?

引用显着的位,这就是为什么他们不允许失败:

这种隐式的失败行为通常用于减少所需的代码量,并且在第一次编写代码时通常不是问题。然而,随着代码从初始开发阶段进入维护阶段,上面的代码可能会导致难以调试的微妙错误。这些错误是由于开发人员添加案例但忘记在块末尾放置中断的常见错误造成的。

在 C# 中,switch 语句要求在 case 结束时进行显式流程控制,可以是break、goto、return 或 throw。如果开发人员需要失败语义,可以通过在 case 语句末尾显式 goto 来实现。

这就是为什么它不是自动的:

由于 C# 规则要求在 case 块末尾(最常见的是中断)进行显式流控制,许多人质疑为什么行为根本没有改变,因此失败不会发生发生。也就是说,不需要中断,只需更改 switch 的语义即可避免案例失败。没有这样做的原因是,非常习惯 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:

This implicit fall-through behavior is often used to reduce the amount of code needed and often isn't an issue the first time that code is written. However, as code moves from the initial development phase into a maintenance phase, the code above can lead to subtle errors that are very hard to debug. These errors result from the very common mistake of the developer adding a case, yet forgetting to put a break at the end of the block.

In C#, the switch statement requires that explicit flow control occur at the end of a case, either a break, goto, return, or throw. If the developer desires fall-through semantics, it can be achieved by an explicit goto at the end of a case statement.

And this is why it's not automatic:

As a result of the C# rules requiring explicit flow-control to occur at the end of a case block (most usually a break), many people question why the behavior simply wasn't changed such that fall-through didn't occur. That is, don't make break required, simply change the semantics of switch to not have fall-through for cases. The reason this wasn't done was so that developers who were very used to C++ wouldn't have a hard time understanding what a switch statement was doing.

违心° 2024-09-14 08:35:38

您可以直接访问,但必须使用 goto 关键字显式这样做:

switch (state) {
    case '1':
        state = '2';
        goto case '2';
    case '2':
        state = '1';
        break;
}

您可以 breakgoto 在 C# 中,但你不能做的是不声明你想要哪个,因为这是难以发现错误的潜在来源。

当您想要 break(或反之亦然)时,发现您的代码说 goto 比发现您忘记添加要容易得多。

这听起来可能很愚蠢,但许多人在花费两个小时寻找 C++ bug 原因的疲惫搜索后,最终会突然意识到您忘记添加 break 并且您的代码一直在失败。 C# 通过强制您声明您想要的内容来避免这种情况。

You are allowed to drop through, but you have to do so explicitly with the goto keyword:

switch (state) {
    case '1':
        state = '2';
        goto case '2';
    case '2':
        state = '1';
        break;
}

You can break or gotoin 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 wanted break (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.

不可一世的女人 2024-09-14 08:35:38

如果你没有案例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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文