As we all know switch cases in c# do not allow you to fall through according the MSDN
Execution of the statement list in the selected switch section begins with the first statement and proceeds through the statement list, typically until a jump statement, such as a break, goto case, return, or throw, is reached. At that point, control is transferred outside the switch statement or to another case label.
Unlike C++, C# does not allow execution to continue from one switch section to the next. The following code causes an error.
If thats the case why does this compile:
void Main()
{
int s = 3;
switch (s)
{
case 1:
case 2:
case 3:
Console.WriteLine("hit 3");
break;
}
}
Shouldn't this be identified as a compile time error?
First off, the code you provided doesn't throw a run time error. Secondly, it falls under a different category (from same MSDN article, emphasis mine):
The difference is whether you have multiple empty
case
statements, that's allowed. But you can't have acase
with code in it, and let it fall through.