3 ms·
You can "goto" any case label in C#, which works like fallthrough but is more flexible (since you can jump anywhere). At the same time, it avoids the refactorin
by pcwalton 15y ago
You can "goto" any case label in C#, which works like fallthrough but is more flexible (since you can jump anywhere). At the same time, it avoids the refactoring hazard associated with fallthrough, in which changing the order of the switch labels can change the behavior of the code. Of the languages that have "switch" (not counting ML/Haskell-style pattern matching here), C#'s approach seems ideal.
- kstenerud 15y agoThe C# approach sounds like a recipe for abuse: switch(something) { case 1: // Do stuff goto case 4; case 2: // Do stuff goto case 1; case 3: // Do stuff goto case 5; case 4: // Do stuff goto case 3; case 5: // Do stuff break; }
- PotatoEngineer 14y agoThat's quite horrible! Fortunately, it's obviously horrible, and since "goto" has such a stigma attached to it, programmers will only use it when it's necessary. (Cross your fingers.)
- mkr-hn 14y agoregister_globals would like to have a word with you.
- natesm 15y agoI agree that removing implicit fallthrough is good, since it's a 90%/10% feature, but my problem is that the "break;" is meaningless extra syntax, since the lack of it generates a compiler error. C# already implies no implicit fallthrough, so there's no need to explicitly state that you're not falling through. A better solution (for every language, really) would be a "fallthrough;" statement or "goto case x;", which C# does get right.