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?