Share via

Switch Statement in C#

Pip 265 Reputation points
2026-02-26T02:43:34.4266667+00:00

Hi,

I'm using .NET 10 , VS 2026.

The Switch Statement have changed over the C# versions.

Can you please summarize how switch statement change over the C# versions ?

Why switch statement input is only string ?

How to use switch statement in .NET10 as best practice?

Can you please supply sniped code ?

Thanks in advance,

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 14,955 Reputation points Microsoft External Staff Moderator
    2026-02-26T06:33:07.9766667+00:00

    Hi @Pip ,

    Thanks for reaching out.

    Quick summary of changes:

    • Older C#: switch mainly worked with int, char, enum, and later string.
    • C# 7+: Introduced pattern matching (you can switch on types and conditions).
    • C# 8+: Added switch expressions - a cleaner, shorter way to write switch logic.
    • Later versions (including .NET 10): Expanded pattern matching.

    So if you’re seeing string used as input, that’s just the example, not a limitation. You can switch on numbers, enums, types, and more.

    Best practice in .NET 10: Use switch expressions when returning a value. It’s cleaner and easier to read.

    Example:

    int number = 2;
    
    string result = number switch
    {
        1 => "One",
        2 => "Two",
        _ => "Other"
    };
    
    Console.WriteLine(result);
    

    And here’s a simple pattern matching example:

    object input = 15;
    
    string message = input switch
    {
        int n when n > 10 => "Greater than 10",
        int n => $"Number {n}",
        string s => $"Text: {s}",
        _ => "Unknown"
    };
    

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.

Answer accepted by question author
  1. Marcin Policht 82,360 Reputation points MVP Volunteer Moderator
    2026-02-26T04:25:29.07+00:00

    Yep - the switch statement in C# has evolved from a simple value-based branching construct into a pattern matching engine.

    In early C# versions (1.0–6.0), switch worked only with constant values of limited types: integral types (int, byte, char, etc.), enum, and later string (added in C# 2.0). Each case had to be a compile-time constant, and pattern matching was not supported. Control flow relied on break, and fall-through was not allowed except for empty cases.

    C# 7.0 introduced pattern matching. Switch could now match on type patterns and declaration patterns, for example matching an object by its runtime type. C# 7.3 improved pattern matching slightly, but the big shift came in C# 8.0 with switch expressions. Instead of a statement block, switch could return a value directly, making it expression-based and more functional in style. This enabled more concise and safer code, especially when combined with expression-bodied members.

    C# 9.0 added relational patterns (<, >, <=, >=), logical patterns (and, or, not), and improved type patterns. C# 10 and C# 11 refined pattern matching with extended property patterns and required members. C# 12 and later versions (used in .NET 8–10 era) continued enhancing list patterns, slice patterns, and recursive patterns, allowing matching against arrays and collections structurally. By .NET 10 / modern C#, switch is essentially a full pattern matching system capable of handling complex object graphs.

    Switch input is not limited to string. That was true only in older versions where supported types were restricted. Today you can switch on almost any type, including object, and use pattern matching to inspect types, properties, ranges, null, collections, and more. The original limitation existed because switch was compiled to jump tables and required constant comparisons. Pattern matching removed that restriction by lowering code to decision trees instead of simple jump tables.

    In .NET 10, the recommendation is to prefer switch expressions when you are computing a value and switch statements when you need side effects or multiple statements per branch. You should use pattern matching instead of large if-else chains. You should also avoid default if you want exhaustiveness checking on enums, because the compiler can warn you when new enum values are added.

    Classic switch statement (old style):

    int number = 2;
    
    switch (number)
    {
        case 1:
            Console.WriteLine("One");
            break;
        case 2:
            Console.WriteLine("Two");
            break;
        default:
            Console.WriteLine("Other");
            break;
    }
    

    Modern switch expression:

    int number = 2;
    
    string result = number switch
    {
        1 => "One",
        2 => "Two",
        _ => "Other"
    };
    
    Console.WriteLine(result);
    

    Pattern matching with types:

    object input = "Hello";
    
    string message = input switch
    {
        string s => $"String of length {s.Length}",
        int i => $"Integer value {i}",
        null => "Null value",
        _ => "Unknown type"
    };
    

    Relational and logical patterns:

    int age = 25;
    
    string category = age switch
    {
        < 13 => "Child",
        >= 13 and < 20 => "Teenager",
        >= 20 and < 65 => "Adult",
        _ => "Senior"
    };
    

    Property pattern:

    public record Person(string Name, int Age);
    
    Person person = new("Alice", 30);
    
    string description = person switch
    {
        { Age: < 18 } => "Minor",
        { Age: >= 18 and < 65 } => "Working age",
        { Age: >= 65 } => "Retired"
    };
    

    List pattern (modern C#):

    int[] numbers = { 1, 2, 3 };
    
    string shape = numbers switch
    {
        [] => "Empty",
        [1] => "Single 1",
        [1, 2, ..] => "Starts with 1,2",
        _ => "Other"
    };
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.