为什么我需要用休息时间吗? [英] Why do I need to use break?

查看:101
本文介绍了为什么我需要用休息时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么C#要求我使用开关语句虽然落空语义根据定义不允许的。因此,编译器会生成在每个情况 - 块的末尾并保存我的麻烦。

I was wondering why C# requires me to use break in a switch statement although a fall-through semantics is by definition not allowed. hence, the compiler could generate the break at the end of each case-block and save me the hassle.

但是,有一种情况(这已经在这个网站讨论)我能来这了,可能是对的明确用途的理由

However, there is one scenario (which has already been discussed on this site) which I could come up with that might be the reason for the explicit usage of break:

switch (foo) {
    case 0:
    case 1:
        bar();
        break;
    default:
        break;
}

下面的方法巴()被称为如果有任何的值为0或1。

Here, the method bar() is called if foo has either the value 0 or 1.

这个选项将在打破这个词的真正意义,如果编译器会生成本身语句。这是它,这就是为什么突破是强制性的还是有什么其他的好理由的理由吗?

This option would break in the truest sense of the word if the compiler would generate break statements by itself. Is this it, is this the reason why the break is compulsory or are there any other good reasons?

推荐答案

我怀疑的原因C#要求开发人员放置在每一种情况下的最后一个休息或终端语句是清晰。

它避免了初来乍到的语言从假设开关()在C#中的行为像开关C或C ++告吹行为发生在哪里。 <罢工>只有在相邻空箱的情况下,不落空发生在C#中 - 这是比较明显的

It avoids newcomers to the language from assuming that switch( ) in C# behaves like switch in C or C++ where fall through behavior occurs. Only in the cases of adjacent empty cases does fall through occur in C# - which is relatively obvious.

编辑:其实,在C#fallthrough是总是非法的。什么是合法的,但是,是具有两个或多个标签相关联的单个情况。 埃里克利珀写在长约这个行为以及如何从C / C ++不同,switch语句的。

Actually, in C# fallthrough is always illegal. What is legal, however, is having a single case associated with two or more labels. Eric Lippert writes at length about this behavior and how it differs from C/C++ switch statements.

您可能有兴趣阅读的this文章

这篇关于为什么我需要用休息时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆