13

It appears I need to use a break in each case block in my switch statement using C#.

I can see the reason for this in other languages where you can fall through to the next case statement.

Is it possible for case blocks to fall through to other case blocks?

Thanks very much, really appreciated!

Russell
  • 17,481
  • 23
  • 81
  • 125

4 Answers4

36

Yes, you can fall through to the next case block in two ways. You can use empty cases, which don't need a break, or you can use goto to jump to the next (or any) case:

switch (n) {
  case 1:
  case 2:
  case 3:
    Console.WriteLine("1, 2 or 3");
    goto case 4;
  case 4:
    Console.WriteLine(4);
    break;
}
yoozer8
  • 7,361
  • 7
  • 58
  • 93
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Empty case statements are not legal and `case 1` does not counter this. See [Eric Lippert's Explanation](http://stackoverflow.com/questions/3047863/how-can-i-use-more-than-one-constant-for-a-switch-case-in-c/3049601#3049601) , as well as my comment mentioning why the distinction is not pedantic. Telling people that empty case blocks are legal [causes them to make mistakes](http://stackoverflow.com/q/8071655/18192). – Brian Nov 09 '11 at 21:50
  • 5
    @Brian: You can call it something else if you like, but the code above is perfectly legal. Strictly technically the code will of course not be implemented as a series of code blocks that you fall through, but that is not the case in any language, and the same can be said about most control structures. The mistake that you link to is not related to empty cases at all (or whatever you like to call them), as it's not empty. – Guffa Nov 09 '11 at 22:19
6

The enforcement of "break" is there to stop bugs. If you need to force a fall-thru then use "goto case " (replace the with appropriate value)

the following example shows what you can do:

switch(n)
{
    case 1:
    case 2:
      //do something for 1+2
      //...
      goto case 3;
    case 3:
      //do something for 3, and also extra for 1+2
      //...
      break;
    default:
      //do something for all other values
      //...
      break;
}

See http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx

Will
  • 1,561
  • 9
  • 6
  • 2
    If it doesn't support fall through, then there is no bug to be prevented. Whether a break is there or not, if the language doesn't support fall through, it doesn't support it, bottom line. Compile time enforcement of `break;` doesn't prevent you from accidentally falling through, because as you said, fall through is not even supported so such an accident cannot occur... – AaronLS May 06 '14 at 19:17
3

C# doesn't support implicit fall through construct, but the break (or goto) nonetheless has to be there (msdn). The only thing you can do is stack cases in the following manner:

switch(something) {
    case 1:
    case 2:
      //do something
      break;
    case 3:
      //do something else
}

but that break (or another jump statement like goto) just needs to be there.

Marek Karbarz
  • 28,956
  • 6
  • 53
  • 73
  • This is not quite accurate. It's not that a "jump" statement (by which I assume you mean break, continue, goto, return or throw) has to be at the end, it's that a statement with an unreachable end point has to be at the end. All the "jump" statements have that property, but there are also other statements with that property. For example, it's perfectly legal, though rare, to end a switch section with "while(true) M();" – Eric Lippert Nov 25 '09 at 15:54
  • 1
    I see that the MSDN documentation is incorrect. I'll have a talk with the documentation manager. – Eric Lippert Nov 25 '09 at 15:55
1

In my C# (.NET 1.1, CF) code, both of these are allowed:

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
        break;
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
        break;
    default:            
        return new ZebraQL220Printer();         
                        break;  
}

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
    default:            
        return new ZebraQL220Printer();         
}

...but with the breaks in, they are grayed out, so considered moot. So, at least in my case, they are allowed but not required.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    A return statement (as well as throwing Exceptions) causes the code to leave the complete method, that´s why a following break statement is not needed then. There is no switch structure to leave anymore. – Udontknow Nov 21 '16 at 11:08