C# - Switch Statement

Updated on:

The switch statement can be used instead of if else statement when you want to test a variable against three or more conditions. Here, you will learn about the switch statement and how to use it efficiently in the C# program.

The following is the general syntax of the switch statement.

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

The switch statement starts with the switch keyword that contains a match expression or a variable in the bracket switch(match expression). The result of this match expression or a variable will be tested against conditions specified as cases, inside the curly braces { }. A case must be specified with the unique constant value and ends with the colon :. Each case includes one or more statements to be executed. The case will be executed if a constant value and the value of a match expression/variable are equal. The switch statement can also contain an optional default label. The default label will be executed if no cases executed. The break, return, or goto keyword is used to exit the program control from a switch case.

The following example demonstrates a simple switch statement.

Example: C# Switch Statement
int x = 10;

switch (x)
{ 
    case 5:
        Console.WriteLine("Value of x is 5");
        break;
    case 10:
        Console.WriteLine("Value of x is 10");
        break;
    case 15:
        Console.WriteLine("Value of x is 15");
        break;
    default:
        Console.WriteLine("Unknown value");
        break;
}
Output:
Value of x is 10

Above, the switch(x) statement includes a variable x whose value will be matched with the value of each case value. The above switch statement contains three cases with constant values 5, 10, and 15. It also contains the default label, which will be executed if none of the case value match with the switch variable/expression. Each case starts after : and includes one statement to be executed. The value of x matches with the second case case 10:, so the output would be Value of x is 10.

Note:
The switch statement can include any non-null expression that returns a value of type: char, string, bool, int, or enum.

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

Example: C# Switch Statement
int x = 125;

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

Switch Case

The switch cases must be unique constant values. It can be bool, char, string, integer, enum, or corresponding nullable type.

Note
C# 7.0 onward, switch cases can include non-unique values. In this case, the first matching case will be executed.

Consider the following example of a simple switch statement.

Example: 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.

Example: C# Combined Switch Cases
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 exit the case explicitly by using break, return, goto statement, or some other way, making sure the program control exits a case and cannot fall through to the default case.

The following use the return keyword.

Example: return in Switch Case
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

The switch cases without break, return, or goto statement or with the same constant values would give a compile-time error.

Example: C# Switch Statement
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 - Control cannot fall through from one case label ('case 1:') to another
        Console.WriteLine($"{x} is odd Value");
    defaut:
        Console.WriteLine($"{x} is odd Value");
        break;
}

Nested Switch Statements

A switch statement can be used inside another switch statement.

Example: Nested switch statements
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
Points to Remember :
  1. The switch statement is an alternative to if else statement.
  2. The switch statement tests a match expression/variable against a set of constants specified as cases.
  3. The switch case must include break, return, goto keyword to exit a case.
  4. The switch can include one optional default label, which will be executed when no case executed.
  5. C# compiler will give errors on missing :, constant value with cases, exit from a case.
  6. C# 7.0 onward, switch cases can include non-unique values. In this case, the first matching case will be executed.