Share via


switch (C# Reference)

The switch statement is a control statement that selects a switch section to execute from a list of candidates.

Each switch section contains one or more case labels and a list of 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 a list of two statements.

Example

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;
}

Each case label specifies a constant value. Control is transferred to the switch section whose case label contains a constant value that matches the value of the switch expression, caseSwitch. 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 label case 1 specifies the value 1, and the value of caseSwitch also is 1.

A switch statement can include any number of switch sections, and each section can have one or more case labels. However, no two case labels can contain the same constant value.

Execution of the statement list in the selected section begins with the first statement and proceeds through the statement list, typically until a jump statement is reached, such as a break, goto case, return, or throw. 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;
}

The requirement in C# is that the end of every switch section, including the final one, is unreachable. Although this requirement usually is met by using a jump statement, the following case also is valid, because the end of the statement list cannot be reached.

case 4:
    while (true)
        Console.WriteLine("Endless looping. . . .");

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, string input is converted to an integer variable, switchExp, which is used for the switch expression. You also can use the string variable, str, directly. To do that, you would change the case labels to specify string values, as is shown in the following code.

switch(str)
{
    case "1":
        // ...
    case "2":
        // ...
}
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 switchExp = int.Parse(str);
        int cost = 0;
        switch (switchExp)
        {
            case 1:
                cost += 25;
                break;
            case 2:
                cost += 25;
                goto case 1;
            case 3:
                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.

See Also

Reference

C# Keywords

Concepts

C# Programming Guide

Other Resources

C# Reference

if-else (C# Reference)