Switch Expressions in C#

From the C# Programming Language cheat sheet · Pattern Matching · verified Jul 2026

Switch Expressions

Modern pattern matching with switch expressions

csharp
// Switch expression
string result = number switch
{
    1 => "One",
    2 => "Two",
    < 0 => "Negative",
    > 100 => "Large",
    _ => "Other"
};

// Property patterns
string description = person switch
{
    { Age: < 18 } => "Minor",
    { Age: >= 65 } => "Senior",
    _ => "Adult"
};

// Type patterns
object obj = GetValue();
string type = obj switch
{
    int i => $"Integer: {i}",
    string s => $"String: {s}",
    null => "Null",
    _ => "Unknown"
};
💡 Switch expressions provide concise pattern matching
⚡ Property patterns match on object properties
📌 Guard clauses (when) add additional conditions
🟢 List patterns enable array/collection matching
Back to the full C# Programming Language cheat sheet