开发者

Why do I need to use break?

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;
}

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?


The question presupposes a falsehood and therefore cannot be answered. The language does NOT require a break at the end of a switch section. The language requires that the statement list of a switch section must have an unreachable end point. "break" is just the most commonly used statement that has this property. It is perfectly legal to end a switch section with "return;", "goto case 2;", "throw new Exception();" or "while (true) {}" (or in some cases, continue, though that's usually a bad idea.)

The reason we require a statement list with an unreachable endpoint is to enforce the "no fall through" rule.

If your question is better stated as "why doesn't the compiler automatically fix my error when I fail to produce a switch section with a statement list with an unreachable end point, by inserting a break statement on my behalf?", then the answer is that C# is not a "make a guess about what the developer probably meant and muddle on through" sort of language. Some languages are -- JScript, for example -- but C# is not.

We believe that C# programmers who make mistakes want to be told about those mistakes so that they can intentionally and deliberately correct them, and that this is a good thing. Our customers tell us that they do not want the compiler to make a guess about what they meant and hope for the best. That's why C# also does not make a guess about where you meant to put that missing semicolon, as JScript does, or silently fail when you attempt to modify a read-only variable, as JScript does, and so on.


I suspect that the reason C# requires the developer to place a break or terminal statement at the end of each case is for clarity.

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.

EDIT: 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.

You may be interested in reading this article on Eric Lipperts blog.


By making break optional, you open yourself up to bugs like this:

switch (thing_result)
{
    case GOOD_THING:
        IssueReward();
        // UH-OH, missing break!
    case BAD_THING:
        IssuePunishment();
        break;
}

The C# language designers have tried to help programmers avoid pitfalls in the languages that came before it, generally by forcing the programmer to be more explicit (e.g. explicit use of the 'override' or 'new' keyword for implementing or shadowing virtual members).


My understanding of the matter is that it was included to match C++ syntax.


If you wouldn't need to append a break, there is a problem

switch (number)
{
    case 0:
    case 1:
        DoSomething();
}

What happens if number == 0? Is this an empty case which does not do anything or will DoSomething() be executed?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜