Select Page

I used to use voluntary fall through a lot in switch statements in C and C++. In C# it is no longer as easy to do as before, since all cases of a C# switch are required to end with either break or goto. You can still do voluntary fall through. You just need to be very specific about it.

Finally an excuse for using goto !!

You can use the goto statement to jump from one case to another in a C# switch. Just replace the usual break statement with a goto pointing at the next case, and you have voluntary fallthrough.

int i = 0;
switch (i)
{
  case 0:
    Console.Write("Voluntary fallthrough ");
    goto case 1;
  case 1:
    Console.Write("is still ");
    goto default;
  default:
    Console.Write("possible");
    break;
}

This code will print out “Voluntary fallthrough is still possible” on the console.

I have always been a big fan of goto statements… Not really because of their usefulness, but more because everyone is always hitting on them. It is a bit like cheering for the underdog in a football match, which i almost always do. I have had a principle of having at least 1 goto statement in all applications i have done for some years now, and with voluntary fall through i may actually have found a reasonable excuse for them :-).