A switch expression returns a value from matching cases.

Switch Expression

SwitchExpression.cs
using System;

class Program
{
    static void Main()
    {
        string command = ;
        string action = command switch
        {
            "start" => "run",
            "stop" => "halt",
            _ => "wait"
        };

        Console.WriteLine($"command={command}");
        Console.WriteLine($"action={action}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string command = ;
        string action = command switch
        {
            "start" => "run",
            "stop" => "halt",
            _ => "wait"
        };

        Console.WriteLine($"command={command}");
        Console.WriteLine($"action={action}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string command = ;
        string action = command switch
        {
            "start" => "run",
            "stop" => "halt",
            _ => "wait"
        };

        Console.WriteLine($"command={command}");
        Console.WriteLine($"action={action}");
    }
}
switch expression A switch expression selects one result based on a matched pattern.