C# Language Reference
switch (C# Reference)

The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body as the following example:

Example

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

Control is transferred to the case statement which matches the value of the switch. The switch statement can include any number of case instances, but no two case statements can have the same value. Execution of the statement body begins at the selected statement and proceeds until the break statement transfers control out of the case body. A jump statement such as a break is required after each case block, including the last block whether it is a case statement or a default statement. With one exception, (unlike the C++ switch statement), C# does not support an implicit fall through from one case label to another. The one exception is if a case statement has no code.

If no case expression matches the switch value, then control is transferred to the statement(s) that follow the optional default label. If there is no default label, control is transferred outside the switch.

C#
    class SwitchTest 
{
    static void Main()
    {
        Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); 
        Console.Write("Please enter your selection: "); 
        string s = Console.ReadLine(); 
        int n = int.Parse(s);
        int cost = 0;
        switch(n)
        {
        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.
    */
  • In the preceding example, an integral type variable, n, was used for the switch cases. Notice that you can also use the string variable, s, directly. In this case, you can use switch cases like these:

switch(s)
{
    case "1":
        // ...
    case "2":
        // ...
}

The following sample shows that fall through from one case label to another is allowed for empty case labels.

C#
class SwitchTest2 
{
    static void Main()
    {
        int n = 2;
        switch(n) 
        {
            case 1:
            case 2: 
            case 3: 
                Console.WriteLine("It's 1, 2, or 3.");
                break; 
        default: 
            Console.WriteLine("Not sure what it is.");
            break; 
        }
    }
}
//  Output: It's 1, 2, or 3.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 5.3.3.6 Switch statement

  • 8.7.2 The switch statement

See Also

Concepts

Reference

Other Resources

Tags :


Community Content

Jim Johnston
Using a return as the jump statement
If you're in a method that returns a value, you can use the switch statement to return a value.

public static string GreetingMessage()
{
string ActivationCountry = "United States";

switch(ActivationCountry)
{
case "Canada":
return "Welcome to Canada";
case "United States":
return "Welcome to the United States";
default:
return "Welcome!";
}
}

This example shows you don't have to always use the break statement to jump out of the switch.

L.Carter
Switch where multiple case's execute the same statement
Here's how you can execute the same statement(s) for multiple case's
The following is best practice on how you should handle cases that do the same logic when hit.
using fall through on the case:
        int x = 1;
switch ( x )
{
case 1:
case 2:
/* code here */
break;
case 3:
/* code here */
break;
default:
break;
}

The following while doable should not be used because it promotes unreadable code, adding to complexity and potential for bugs:
Or use goto case statements:
        int x = 1;
switch ( x )
{
case 1:
goto case 2;
case 2:
goto case 5;
break;
case 3:
/* code here */
break;
case 4:
goto case 2;
break;
case 5:
/* code here */
break;
case 6:
/* code here */
break;
case 7:
goto case 5;
break;
case 8:
/* code here */
break;
case 9:
goto case 2;
break;
default:
break;
}

Page view tracker