Simplifying Switch Statements with Switch Expressions in C# 7.0
In C# 7.0, the switch expression was introduced, providing a more concise and readable way to evaluate a single expression from a list of candidate expressions based on pattern matching with an input expression. This article explores the usage of switch expressions and highlights their advantages over traditional switch statements. We’ll also demonstrate real-world examples to showcase the power of switch expressions.
Traditional Switch Statements:
In earlier versions of C#, switch statements were commonly used instead of if/else if/else logic. Let’s consider the following example code snippet:
public void Debug_Old(bool? state)
{
string message = string.Empty;
switch (state)
{
case true:
message = "Debug Enabled";
break;
case false:
message = "Debug Disabled";
break;
default:
message = "Invalid";
break;
}
Console.WriteLine(message);
}
Introducing Switch Expressions:
With C# 7.0, switch expressions provide a more compact and readable alternative. The same functionality can be achieved with the following code:
void Debug(bool? state)
{
var message = state switch
{
true => "Debug Enabled", //_arms_
false => "Debug Disabled",
_ => "Invalid State" //case guard
};
Console.WriteLine(message);
}
Switch Expression Features:
The switch expression syntax offers several notable features:
-
Concise Syntax: The switch expression eliminates the need for individual case statements and break statements. It condenses the logic into a single expression, making the code more concise and readable.
-
Default Case: The default case, represented by the underscore (_) symbol, handles any unmatched cases. It must be the last expression in the switch expression.
-
Type Compatibility: Switch expressions are compatible with bool, char, string, integral types, enumerations, and corresponding nullable types. However, switching by an object variable is not supported in versions prior to C# 7.0.
Real-World Examples:
Let’s explore a couple of real-world scenarios to showcase the practical usage of switch expressions.
Example 1- FizzBuzz
The FizzBuzz problem is a common interview question. We can solve it using switch expressions:
string FizzBuzz(int i)
{
return (i % 3, i % 5) switch
{
(0, 0) => "FizzBuzz",
(0, _) => "Fizz",
(_, 0) => "Buzz",
_ => i.ToString()
};
}
Recursive switch expression
Switch expressions can also be used recursively, as shown in this factorial calculation example:
public int Factorial(int n)
{
return n switch
{
1 => 1,
_ => n * Factorial(n - 1)
};
}
Conclusion:
Switch expressions introduced in C# 7.0 offer a more concise and readable alternative to traditional switch statements. By leveraging pattern matching, switch expressions provide a powerful tool for simplifying code and improving code maintainability. This article has covered the basic usage of switch expressions and demonstrated real-world examples to illustrate their effectiveness. Consider utilizing switch expressions in your C# projects to enhance code readability and productivity.