Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


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


That'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.)


register_globals would like to have a word with you.


I 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.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: