switch (C# Reference)
The switch statement is a control statement that selects a switch section to execute from a list of candidates.
A switch statement includes one or more switch sections. Each switch section contains one or more case labels followed by one or more statements. The following example shows a simple switch statement that has three switch sections. Each switch section has one case label, such as case 1, and two statements.
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
Remarks
Each case label specifies a constant value. The switch statement transfers control to the switch section whose case label matches the value of the switch expression (caseSwitch in the example). If no case label contains a matching value, control is transferred to the default section, if there is one. If there is no default section, no action is taken and control is transferred outside the switch statement. In the previous example, the statements in the first switch section are executed because case 1 matches the value of caseSwitch.
A switch statement can include any number of switch sections, and each section can have one or more case labels (as shown in the string case labels example below). However, no two case labels may contain the same constant value.
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.
switch (caseSwitch)
{
// The following switch section causes an error.
case 1:
Console.WriteLine("Case 1...");
// Add a break or other jump statement here.
case 2:
Console.WriteLine("... and/or Case 2");
break;
}
C# requires the end of switch sections, including the final one, to be unreachable. That is, unlike some other languages, your code may not fall through into the next switch section. Although this requirement is usually met by using a break statement, the following case is also valid, because it ensures that the end of the statement list cannot be reached.
case 4:
while (true)
Console.WriteLine("Endless looping. . . .");
Example
The following example illustrates the requirements and capabilities of a switch statement.
class Program
{
static void Main(string[] args)
{
int switchExpression = 3;
switch (switchExpression)
{
// A switch section can have more than one case label.
case 0:
case 1:
Console.WriteLine("Case 0 or 1");
// Most switch sections contain a jump statement, such as
// a break, goto, or return. The end of the statement list
// must be unreachable.
break;
case 2:
Console.WriteLine("Case 2");
break;
// The following line causes a warning.
Console.WriteLine("Unreachable code");
// 7 - 4 in the following line evaluates to 3.
case 7 - 4:
Console.WriteLine("Case 3");
break;
// If the value of switchExpression is not 0, 1, 2, or 3, the
// default case is executed.
default:
Console.WriteLine("Default case (optional)");
// You cannot "fall through" any switch section, including
// the last one.
break;
}
}
}
In the final example, the string variable, str, and string case labels control the flow of execution.
class SwitchTest
{
static void Main()
{
Console.WriteLine("Coffee sizes: 1=small 2=medium 3=large");
Console.Write("Please enter your selection: ");
string str = Console.ReadLine();
int cost = 0;
// Notice the goto statements in cases 2 and 3. The base cost of 25
// cents is added to the additional cost for the medium and large sizes.
switch (str)
{
case "1":
case "small":
cost += 25;
break;
case "2":
case "medium":
cost += 25;
goto case "1";
case "3":
case "large":
cost += 50;
goto case "1";
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
if (cost != 0)
{
Console.WriteLine("Please insert {0} cents.", cost);
}
Console.WriteLine("Thank you for your business.");
}
}
/*
Sample Input: 2
Sample Output:
Coffee sizes: 1=small 2=medium 3=large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.
*/
C# Language Specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.