Why the c# compiler requires the break statement in switch construction?
The compiler doesn't so much 'need' the break statements, it demands them.
This was a design decision. It keeps the code semantically close to C and C++ while eliminating the pitfalls of fall-through that was always a debatable 'feature' of the C languages.
The break statement in c# was a design decision by the creators of the language...Essentially they wanted an "unambiguous" break statement, a break statement that would only work one way. In short, they didn't want fall-through, and if they had just prevented fall-through without including "break," it would have broken backwards compatibility with c++.
Fallthrough is allowed if the case expression is empty:
case Foo: // fallthrough allowed.
case Bar:
Console.WriteLine ("Foo or Bar");
break; // required
That it is not allowed is a common misconception in the same league as "you can't assign values in if-conditions" *
* You can. The rule is just that only boolean values are allowed in if-conditions, and x=false
with bool x;
is a boolean value.