English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Switch Statement

When you need to test a variable against three or more conditions, you can use a switch statement instead of if-else statements. Here, you will learn about the switch statement and how to use it effectively in a C# program.

Here is the general syntax of the switch statement.

 Syntax: }}

switch(match expression/variable)
{
    case constant-value:
        //The statement to be executed;
        break;
    default: 
        //The statement to be executed;
        break;
}

A switch statement starts with the switch keyword, which contains a matching expression or a variable in parentheses switch(matching expression match expression). The result of this matching expression or variable will be tested against the case conditions specified within the curly braces {}./If the value of the variable is equal, the case will be executed. A switch statement can also include an optional default label. If no case is executed, the default label is executed. The break, return, or goto keywords are used to exit the program control from the switch case.

The following example demonstrates a simple switch statement.

int x = 10;
switch (x
{ 
    case 5:
        Console.WriteLine("x's value is5");
        break;
    case 10:
        Console.WriteLine("x's value is10");
        break;
    case 15:
        Console.WriteLine("x's value is15");
        break;
    default:
        Console.WriteLine("Unknown value");
        break;
}
Output:
x's value is10

Above, the switch (x) statement contains a variable x whose value will be compared with each case value matches. The above switch statement contains constant values5,10and15three cases. It also includes a default label, if no case value matches the switch variable/Expression matches, the label will be executed. Each case starts after :, and contains a statement to be executed. The value of X matches the value in the second case10: Matching, the output value is10

A switch statement can include any non-empty expression that returns a value of type char, string, bool, int, or enum.

A switch statement can also include an expression, whose result will be tested against each case at runtime.

int x = 125;
switch (x % 2)
{ 
    case 0:
        Console.WriteLine($"{x} is even");
        break;
    case 1:
        Console.WriteLine($"{x} is odd");
        break;
}
Output:
125is odd

Switch Case

Switch Case must be a unique constant value. It can be bool, char, string, integer, enum, or the corresponding nullable type.

Note

In C# 7After .0, a switch case can include non-unique values. In this case, the first matching case will be executed.

Consider the following simple example of a switch statement.

string statementType = "switch";
switch (statementType)
{
    case "if.else":
        Console.WriteLine("if...else statement");
        break;
    case "ternary":
        Console.WriteLine("Ternary operator");
        break;
    case "switch":
        Console.WriteLine("switch statement");
        break;
}
Output:
switch statement

Multiple cases can be combined to execute the same statements.

int x = 5;
switch (x
{ 
    case 1:
        Console.WriteLine("x = 1");
        break;
    case 2:
        Console.WriteLine("x = 2");
        break;
    case 4:
    case 5:
        Console.WriteLine("x = 4 or x = 5");
        break;
    default:
        Console.WriteLine("x > 5");
        break;
}

Each case must explicitly exit using break, return, goto statements, or other means to ensure that program control exits one case and does not enter the default case.

The following uses the return keyword.

static void Main(string[] args)
{
    int x = 125;
    Console.Write( isOdd(x)? "Even value" : "Odd value");
}
static bool isOdd(int i, int j)
{
    switch (x % 2)
    { 
        case 0:
            return true;
        case 1:
            return false;
        default:
            return false;
    }
    
    return false;
}
Output:
Odd value

Omitting break, return, or goto statements or having switch cases with the same constant values will result in a compile-time error.

int x = 1;
switch (x
{ 
    case 0:
        Console.WriteLine($"{x} is even value");
        break;
    case 1:
        Console.WriteLine($"{x} is odd Value");
        break;
    case 1: // Error-Controls cannot be moved from one case label(“ case 1:)Enter another case label
        Console.WriteLine($"{x} is odd Value");
    defaut:
        Console.WriteLine($"{x} is odd Value");
        break;
}

Nested switch statements

A switch statement can be used within another switch statement.

int j = 5;
switch (j
{
    case 5:
        Console.WriteLine(5);
        switch (j - 1)
        {
            case 4:
            Console.WriteLine(4);
            switch (j - 2)
            {
                case 3:
                Console.WriteLine(3);
                break;
            }
            break;
        }
        break;
    case 10:
        Console.WriteLine(10);
        break;
    case 15:
        Console.WriteLine(15);
        break;
    default:
        Console.WriteLine(100);
        break;
}
Output:
5 
4 
3
 Key points to remember:
  1. The switch statement is an alternative to the if-else statement.

  2. The switch statement tests the expression against a set of constants specified as case./Variable.

  3. The switch case must contain break, return, or goto keywords to exit the case.

  4. A switch can include an optional default label, which will be executed when no case is executed.

  5. The C# compiler will report an error about missing :, constant values, and exit the case.

  6. From C#7Starting from version .0, the switch condition can include non-unique values. In this case, the first matching case will be executed.