Switch Expression in C#

In C#, utilizing a switch statement enables you to selectively execute various code sections depending on the value of a variable. The switch statement is frequently employed when there are multiple cases to manage, each necessitating a distinct set of operations.

Starting from C# 8.0 and subsequent versions, the switch statement in C# has been improved with a fresh syntax known as switch expressions. This enhanced syntax permits the switch statement to be used as an expression, resembling the functionality of the ternary conditional operator (?:). By leveraging this new syntax, you can conveniently assign a value based on different cases, resulting in more concise and expressive code.

Tools which I have used for this sample.

  1. VS 2022 Community Edition 17.6.2
  2. .NET 7.0

The source code can be downloaded from GitHub.

Let me provide you with an illustrative example showcasing a switch statement:

public static string GetWeekDayByNumberUsingSwitchCase(int day)
{
    var weekDay = string.Empty;
    switch (day)
    {
        case 1:
            weekDay = "Monday";
            break;
        case 2:
            weekDay = "Tuesday";
            break;
        case 3:
            weekDay = "Wednesday";
            break;
        case 4:
            weekDay = "Thursday";
            break;
        case 5:
            weekDay = "Friday";
            break;
        case 6:
            weekDay = "Saturday";
            break;
        case 7:
            weekDay = "Sunday";
            break;
        default:
            weekDay = "Sorry, It's hard to find";
            break;
    }
    return weekDay;
}

In this example, we compare the value of the variable 'day' with each case and execute the respective code block when a match is identified. To ensure proper execution flow, we utilize the 'break' statement to exit the switch block after processing each case. Furthermore, if none of the preceding cases match the value of 'day', the 'default' case is triggered for execution.

Now, let us look at an instance demonstrating the usage of a switch expression:

public static string GetWeekDayByNumberUsingSwitchExpression(int day)
{
    var dayName = day switch
    {
        1 => "Monday",
        2 => "Tuesday",
        3 => "Wednesday",
        4 => "Thursday",
        5 => "Friday",
        6 => "Saturday",
        7 => "Sunday",
        _ => "Sorry, It's hard to find"
    };
    return dayName;
}

In this switch expression, we compare the variable 'day' with each case, and the corresponding expression on the right side of the '=>' arrow is assessed and assigned to the variable 'dayName'. The underscore symbol (_) represents the 'default' case, which functions similarly to the 'default' keyword in the switch statement.

Switch expressions offer a more compact syntax in comparison to switch statements, particularly when you aim to assign a value based on a specific case. They prove particularly valuable in situations where the objective is to assign a value to a variable rather than executing blocks of code.

Next Recommended Reading Understanding Lambda Expressions in C#