Special Offer: My C#/.NET Bootcamp Course is out now. Get 10% OFF using the code FRIENDS10.

Switch Expressions are a new compiler feature available starting with C# 8. It’s a helpful evolution of Switch Statements. In this video, we’ll be looking at how Switch Expressions are different from Switch Statements and how to use them in your code.

C# 8 offers a new syntax to write Switch Statements. It’s called Switch Expressions. Let’s jump right into Visual Studio.

The Switch Statement example 

In this program, we want to get the shipping price for a fictional order system. We call a GetShippingPrice method and provide an instance of a ShippingType enum as its argument.

using System;

var shippingPrice = GetShippingPrice(ShippingType.Standard);
Console.WriteLine("Shipping is: " + shippingPrice);

decimal GetShippingPrice(ShippingType shipping)
{
    switch (shipping)
    {
        case ShippingType.Standard:
            return 8.50m;
        case ShippingType.LowCost:
            return 5.20m;
        case ShippingType.Express:
            return 19.80m;
        default:
            return 8.50m;
    }
}

public enum ShippingType
{
    Standard,
    LowCost,
    Express
}

Within the GetShippingPrice method, we have a switch statement handling all three shipping types. It also handles the default case, which returns the same value as the standard shipping.

Let’s run the program to see the expected output in the console.

Output: 8.50

Rewriting the Switch Statement into a Switch Expression

So far, so good, now how can Switch Expressions help us in this case? First, let’s rewrite the Switch Statement into a Switch Expression.

using System;

var shippingPrice = GetShippingPrice(ShippingType.Standard);
Console.WriteLine("Shipping is: " + shippingPrice);

decimal GetShippingPrice(ShippingType shipping)
{
    var shippingPrice = shipping switch
    {
        ShippingType.Standard => 8.50m,
        ShippingType.LowCost => 5.20m,
        ShippingType.Express => 19.80m,
        _ => 8.50m
    };
    return shippingPrice;
}

public enum ShippingType
{
    Standard,
    LowCost,
    Express
}

A Switch Expression always returns a value. We create a shippingPrice variable to hold the result. Next, we need to swap the switch keyword and its argument.

The last step is to rewrite our cases as expressions. We do this by removing the case keyword and using the arrow syntax instead of the colon to define an expression. At the end of every case, we use a comma instead of a semicolon. We can remove the return keyword because an expression always evaluates to a result.

Switch expressions use the discard operator instead of the default keyword. Let’s rewrite the default case into an expression as well.

Last but not least, let’s change the return definition to use the shippingPrice variable.

Let’s run the program again. As we can see, we still get the same value in the console output.

Output: 8.50

We could also remove the shippingPrice variable and directly return the result of the switch expression.

using System;

var shippingPrice = GetShippingPrice(ShippingType.Standard);
Console.WriteLine("Shipping is: " + shippingPrice);

decimal GetShippingPrice(ShippingType shipping)
{
    return shipping switch
    {
        ShippingType.Standard => 8.50m,
        ShippingType.LowCost => 5.20m,
        ShippingType.Express => 19.80m,
        _ => 8.50m
    };
}

public enum ShippingType
{
    Standard,
    LowCost,
    Express
}

Switch Expressions Rules

Now let’s talk about what we need to know about Switch Expressions.

  • First of all, order matters. The runtime evaluates each expression from top to bottom. It works the same as a traditional switch statement.
  • What’s different is that every expression needs to return a value. For example, what if we want to write the value to the console instead of returning it? Let’s try it. We get a compiler error.
  • An expression has to be a single statement. We cannot use anonymous methods or other delegate definitions.

That’s it for today. We learned about Switch Expressions and how to use them in our C# projects. Let me know if you will use Switch Expression in your code in the comments below.

By the way, in case you wonder why I don’t have a public void Main method in my console application, check out my video about top-level statements in C# 9.

 

Claudio Bernasconi

I'm an enthusiastic Software Engineer with a passion for teaching .NET development on YouTube, writing articles about my journey on my blog, and making people smile.