Skip to content

Syntax of Switch Expressions

A switch expression uses => to map patterns to results.

int number = 5;
string parity = number % 2 == 0 ? "Even" : "Odd";
string description = number
switch
{
< 0 => "Negative",
0 => "Zero",
> 0 => "Positive"
};
Console.WriteLine($"{parity}, {description}");
Terminal window
dotnet run
Terminal window
Odd, Positive