ICYMI C# 8 New Features: Switch Expressions

In the first part of this series on what was introduced in C# 8, we’re going to take a look at switch expressions.

Switch expressions allow you to write fewer lines of code when making use of switch statements. This is useful if you have a switch statement that sets/returns a value based on the input.

Prior to C# 8, the following code could be used to convert an int to its string equivalent:

string word;
switch (number)
{
    case 1:
        word = "one";
        break;
    case 2:
        word = "two";
        break;
    case 3:
        word = "three";
        break;
    default:
        throw new ArgumentOutOfRangeException(nameof(number));                    
}

In the preceding code if the input int number is not 1,2, or 3 an exception is thrown, otherwise the variable word is set to the string representation “one”, “two”, or “three”.

From C# 8 we could instead use a switch expression. A switch expression returns a value, this means we can return the string into the word variable as follows:

string word = number switch
{
    1 => "one",
    2 => "two",
    3 => "three",
    _ => throw new ArgumentOutOfRangeException(nameof(number))
};

Compare this version with first version and you can see we have a lot less code, we don’t have all the repetitive case and breaks.

Also notice that the default block has been replaced with an expression that throws the exception. Also notice that the code makes use of a discard _ as we don’t care about the value. (Discards are “placeholder variables that are intentionally unused in application code” (MS)).

If you want to fill in the gaps in your C# knowledge be sure to check out my C# Tips and Traps training course from Pluralsight – get started with a free trial.

SHARE:

Comments (8) -

  • Steve Hansen

    1/26/2021 7:47:35 PM | Reply

    You don't need to do the word = "one" inside  your switch cases as the switch expression will return the result (it's an expression, not a statement)

    string word = number switch
    {
        1 => "one",
        2 => "two",
        3 => "three",
        _ => throw new ArgumentOutOfRangeException(nameof(number))
    };

    • Jason Roberts

      1/28/2021 5:17:26 AM | Reply

      Thanks Steve, you are correct, sorry for the confusion (that's what I get for copy/paste without proper proof reading) - have updated/

  • Duncan Smart

    1/27/2021 1:26:41 PM | Reply

    Because it's an *expression*, the example can be simplified to :
    string word = number switch
    {
        1 => "one",
        2 => "two",
        3 => "three",
        _ => throw new ArgumentOutOfRangeException(nameof(number))
    };

    • Jason Roberts

      1/28/2021 5:18:30 AM | Reply

      Thanks Duncan, sorry for the confusion (and my copy/pasteing with no proof) - updated now.

  • David Henley

    1/27/2021 3:50:02 PM | Reply

    Shouldn't this just be

    string word = number switch
    {
        1 =>  "one",
        2 => "two",
        3 => "three",
        _ => throw new ArgumentOutOfRangeException(nameof(number))
    };

    • Jason Roberts

      1/28/2021 5:19:47 AM | Reply

      Hi David, sorry for the confusion (I should have proof read the code after copy/paste you are correct), sorry for any confusion - code updated now.

  • Kevin

    1/27/2021 8:19:58 PM | Reply

    In your switch expression example you have cases that include: `word = "one"` isn't that redundant since the result of the expression is assigned to the word variable already?

    • Jason Roberts

      1/28/2021 5:20:56 AM | Reply

      Thanks for pointing out the copy/paste typos Kevin, sorry for any confusion - have updated the code to be correct now.

Pingbacks and trackbacks (1)+

Add comment

Loading