C# 12's Switch Expressions: A More Powerful Alternative to Traditional Switch Statements

C# 12's switch expressions offer a more powerful alternative to traditional switch statements, allowing developers to write more concise and expressive code.

In traditional switch statements, each case is a statement that performs a specific action. However, in switch expressions, each case is an expression that returns a value. This allows developers to write more complex logic in a single line of code.

Let's take a look at an example. Consider the following traditional switch statement:

string color = "red";
string result;

switch (color)
{
    case "red":
        result = "The color is red.";
        break;
    case "green":
        result = "The color is green.";
        break;
    case "blue":
        result = "The color is blue.";
        break;
    default:
        result = "Unknown color.";
        break;
}

This code assigns a value to the result variable based on the value of the color variable. With switch expressions, we can achieve the same result in a much more concise way:

string color = "red";
string result = color switch
{
    "red" => "The color is red.",
    "green" => "The color is green.",
    "blue" => "The color is blue.",
    _ => "Unknown color."
};

Here, we're using the switch keyword followed by the variable we're switching on (color). Then, we have a set of cases, each represented by an arrow (=>) followed by the expression we want to return if that case matches. The final case, represented by _, is the default case.

One of the major benefits of switch expressions is that they can be used in places where traditional switch statements cannot. For example, we can use switch expressions as part of a method call:

string color = "red";
Console.WriteLine(color switch
{
    "red" => "The color is red.",
    "green" => "The color is green.",
    "blue" => "The color is blue.",
    _ => "Unknown color."
});

This code calls the Console.WriteLine method with the result of the switch expression as its argument. This powerful feature allows developers to write more expressive and concise code.

Another benefit of switch expressions is that they can be used with pattern matching, a powerful feature introduced in C# 7.0. This allows us to match values based on their structure, not just their value. For example, we could write a switch expression that matches a tuple:

var point = (1, 2);
string quadrant = point switch
{
    (x, y) when x > 0 && y > 0 => "Quadrant 1",
    (x, y) when x < 0 && y > 0 => "Quadrant 2",
    (x, y) when x < 0 && y < 0 => "Quadrant 3",
    (x, y) when x > 0 && y < 0 => "Quadrant 4",
    _ => "On an axis"
};

This code assigns a value to the quadrant variable based on the values of the x and y components of the point tuple.

In conclusion, C# 12's switch expressions offer a more powerful and concise alternative to traditional switch statements. They allow developers to write more expressive code and can be used in places where traditional switch statements cannot. With the added benefit of pattern matching, switch expressions are a valuable addition to the C# language.


Similar Articles