Switch Expression in C#

Osama HaiDer
2 min readAug 4, 2024

--

In C#, the switch statement is a powerful tool for decision-making. With the introduction of the switch expression in C# 8.0, writing switch statements have become more concise and readable. Let’s use examples to explore both the traditional switch statement and the modern switch expression.

Traditional Switch Statement

First, let’s see how a traditional switch statement works. We’ll use a simple example where we evaluate an integer and return a corresponding string.

Example 1: Traditional Switch Statement

int number = 3;
string result;

switch (number)
{
case 1:
result = "Number is one";
break;
case 2:
result = "Number is two";
break;
case 3:
result = "Number is three";
break;
default:
result = "Number is not one, two, or three";
break;
}

Console.WriteLine(result);

In this example, the number variable is checked against different cases, and the corresponding string is assigned to result.

Switch Expression

Now, let’s convert the above example into a switch expression, which is more compact and easier to read.

Example 2: Switch Expression

int number = 3;
string result = number switch
{
1 => "Number is one",
2 => "Number is two",
3 => "Number is three",
_ => "Number is not one, two, or three"
};

Console.WriteLine(result);

The switch expression evaluates the number variable and directly returns the corresponding string, making the code more concise.

Another Example with Enums

Let’s consider another example using enums to see the difference between a traditional switch statement and a switch expression.

Example 3: Traditional Switch Statement with Enums

public enum DayOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

DayOfWeek today = DayOfWeek.Wednesday;
string message;

switch (today)
{
case DayOfWeek.Monday:
message = "Start of the workweek";
break;
case DayOfWeek.Friday:
message = "Almost the weekend";
break;
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
message = "Weekend!";
break;
default:
message = "Just another weekday";
break;
}

Console.WriteLine(message);

In this example, we use a switch statement to assign a message based on the value of the today variable.

Example 4: Switch Expression with Enums

public enum DayOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

DayOfWeek today = DayOfWeek.Wednesday;

string message = today switch
{
DayOfWeek.Monday => "Start of the workweek",
DayOfWeek.Friday => "Almost the weekend",
DayOfWeek.Saturday or DayOfWeek.Sunday => "Weekend!",
_ => "Just another weekday"
};

Console.WriteLine(message);

With the switch expression, the code becomes shorter and more readable, directly returning the message based on the today variable.

Conclusion

The switch expression in C# provides a more concise and readable way to perform switch operations than the traditional switch statement. It simplifies the code and makes it easier to maintain. By using switch expressions, you can write cleaner and more efficient code.

--

--

Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#