Switch Case in C
Introduction
The switch case in C# has been a staple of control flow since the early days of the language. Traditionally, developers used switch statements to match a variable against a set of constant values and execute the appropriate block of code. However, with the release of C# 8.0 and beyond, Microsoft introduced significant enhancements to the C# switch syntax—making it more expressive, powerful, and readable.
If you're still writing switch statements the old-fashioned way, it's time to level up. In this article, we’ll walk through the modern features of the C# switch introduced in C# 8.0 and later, compare them with the classic switch case, and explore real-world use cases that benefit from the improvements.
The Traditional Switch Case in C
Before diving into what's new, let’s review the classic switch case in C#:
int day = 3;
switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    default:
        Console.WriteLine("Another day");
        break;
}
This form is straightforward but limited. Each case can only match a constant, and you need to remember break statements to avoid fall-through errors. As applications grew more complex, developers needed more flexibility—and C# delivered.
What’s New in C# 8.0 and Later?
Starting with C# 8.0, the switch statement evolved into a switch expression. It’s more concise, supports pattern matching, and eliminates the need for repetitive boilerplate code.
1. Switch Expressions
Instead of using a block with multiple case statements, you can now use a switch expression to return a value directly.
Example:
string day = dayOfWeek switch
{
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    _ => "Another day"
};
Benefits:
- Cleaner, more readable syntax
- No need for breakorreturnstatements
- Expressions return values directly, making it easy to assign results
2. Pattern Matching
With C# 8.0 and beyond, C# switch supports pattern matching, which allows you to match based on more than just values.
Type Patterns:
object obj = 123;
string result = obj switch
{
    int i => $"Integer: {i}",
    string s => $"String: {s}",
    _ => "Unknown type"
};
Property Patterns (C# 9.0+):
You can match based on object properties.
Person person = new Person { Name = "Alice", Age = 30 };
string category = person switch
{
    { Age: < 18 } => "Minor",
    { Age: >= 18 and < 65 } => "Adult",
    { Age: >= 65 } => "Senior",
    _ => "Unknown"
};
3. Relational and Logical Patterns (C# 9.0+)
These patterns allow you to use operators like <, >, and logical combinations such as and, or, and not.
int score = 85;
string grade = score switch
{
    < 60 => "Fail",
    >= 60 and < 70 => "D",
    >= 70 and < 80 => "C",
    >= 80 and < 90 => "B",
    >= 90 => "A",
    _ => "Invalid score"
};
This is far more elegant than nested if-else or long switch-case statements.
  
  
  4. Discard Pattern (_)
The underscore _ is used as a catch-all for unmatched cases, much like default in traditional switches—but cleaner and used consistently across patterns.
Why These Improvements Matter
The modern switch case in C# is more expressive, maintainable, and functional in style. With fewer lines of code, you can perform complex evaluations and return results directly. It fits naturally with the evolution of C# toward more declarative and pattern-oriented programming.
These new features also reduce common bugs like forgetting break statements, and they allow your code to evolve more gracefully as requirements change. No more deeply nested if-else blocks or clunky logic trees—just clean, readable expressions.
When Should You Use the New Switch Expression?
You should consider the new C# switch features when:
- You need to return a value based on multiple conditions.
- You’re working with types, not just constants.
- You want to replace verbose if-elsechains.
- You want cleaner, more maintainable code.
They’re especially useful in areas like:
- Object classification
- Enum value mapping
- Type checking and casting
- Conditional formatting or logging
Final Thoughts
The evolution of the switch case in C# starting with version 8.0 has brought significant improvements that align with modern coding practices. The introduction of switch expressions, pattern matching, and relational logic brings power and flexibility to a once-basic control structure.
Whether you’re new to C# or an experienced developer catching up with recent features, exploring the new C# switch syntax is well worth your time. It not only improves the way you write code but also how readable, efficient, and scalable your applications become.
Now that you’ve had a solid first look, try rewriting your older switch statements using expressions. You'll quickly see how much cleaner your code becomes.
 


 
    
Top comments (0)