Partager via


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:

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

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.

Example

// statements_switch.cs
using System;
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.");
    }
}

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.

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

// statements_switch2.cs
using System;
class SwitchTest 
{
    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.

Code Discussion

  • 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":
        // ...
}

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

Reference

C# Keywords
The switch Statement
if-else (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference