switch Expression

noun

say it: “SWICH ek-SPRESH-un” /swɪtʃ ɪkˈsprɛʃən/

A C# expression that matches a value against patterns and gives back the result of the first match.

What is switch Expression in C#?

switch Expression — An expression that selects and returns a value by matching an input against patterns.

class Shirt
{
    string SizeName(string size)
    {
        return size switch
        {
            "M" => "Medium",
            _ => "Other"
        };
    }
}

In a sentence

  • “A switch expression turns the size letter into its full name in four short lines.”

Level: Novice · More C# terms at this level

Updated