Do empty case statements in C# combine with the next non-empty one?
I originally said:
The question supposes a falsehood. There is no such thing as an empty switch section in a legal C# program.
That is not quite true. The C# specification requires that a switch section contain at least one statement, but the implementation actually does not.
I emend my statement as follows:
The question supposes that there is an empty switch section (note that switch sections are not statements), but there can only be an empty switch section in a very unusual situation. First off, let's clearly describe the anatomy of a switch statement.
When you have:
switch(x)
{
case 1:
case 2:
Console.WriteLine("Hello");
break;
}
there is only one switch section. There is no "empty section" following case 1; a section consists of all the case clauses followed by zero or more statements. (The spec requires one or more statements but the implementation actually allows zero statements.) The statement list must have a non-reachable endpoint.
The only way for there to be an empty statement list whose end point is not reachable inside a switch section is for the switch section itself to be unreachable. There are two ways that can happen. Either the entire switch can be unreachable, or the switch can be switching on a constant. In short, the only way to make an empty switch section is either:
switch(1) { case 2: }
or
if (false) switch(x) { case 2: }
Neither is legal according to the spec, but the C# compiler allows both. Fortunately this is a pretty harmless bug.
In both cases the end point of the section is not reachable and so the empty section is reachable. But it is never the case that there is an empty section between cases.