Switch Case Statement in C#: Syntax and Usage Explained
Table of Content:
The switch.case statement is good for selecting one branch of execution from a set of mutually exclusive ones. It takes the form of a switch argument followed by a series of case clauses. When the expression in the switch argument evaluates to one of the values beside a case clause, the code immediately following the case clause executes. This is one example where you don't need to use curly braces to join statements into blocks; instead, you mark the end of the code for each case using the break statement. You can also include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases. The following switch statement tests the value of the integerA variable:
switch (integerA)
{
case 1:
Console.WriteLine("integerA =1");
break;
case 2:
Console.WriteLine("integerA =2");
break;
case 3:
Console.WriteLine("integerA =3");
break;
default:
Console.WriteLine("integerA is not 1,2, or 3");
break;
}
Note that the case values must be constant expressions; variables are not permitted.
Though the switch.case statement should be familiar to C and C++ programmers, C#'s switch.case is a bit safer than its C++ equivalent. Specifically, it prohibits fall-through conditions in almost all cases. This means that if a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired, too. The compiler enforces this restriction by flagging every case clause that is not equipped with a break statement as an error similar to this:
Control cannot fall through from one case label ('case 2:') to another
Although it is true that fall-through behavior is desirable in a limited number of situations, in the vast majority of cases, it is unintended and results in a logical error that's hard to spot. Isn't it better to code for the norm rather than for the exception?
By getting creative with goto statements, however, you can duplicate fall-through functionality in your switch.cases. But, if you find yourself really wanting to, you probably should reconsider your approach. The following code illustrates both how to use goto to simulate fall-through, and how messy the resultant code can get:
// assume country and language are of type string
switch(country)
{
case "America":
CallAmericanOnlyMethod();
goto case "Britain";
case "France":
language = "French";
break;
case "Britain":
language = "English";
break;
}
There is one exception to the no-fall-through rule, however, in that you can fall through from one case to the next if that case is empty. This allows you to treat two or more cases in an identical way (without the need for goto statements):
switch(country)
{
case "au":
case "uk":
case "us":
language = "English";
break;
case "at":
case "de":
language = "German";
break;
}
One intriguing point about the switch statement in C# is that the order of the cases doesn't matter — you can even put the default case first! As a result, no two cases can be the same. This includes different constants that have the same value, so you can't, for example, do this:
// assume country is of type string
const string england = "uk";
const string britain = "uk";
switch(country)
{
case england:
case britain: // This will cause a compilation error.
language = "English";
break;
}
The previous code also shows another way in which the switch statement is different in C# compared to C++: In C#, you are allowed to use a string as the variable being tested.