How to Use Pattern Matching in Switch in C#
In C#, use
switch with pattern matching by specifying patterns like type patterns, constant patterns, or property patterns in case labels. This lets you check an object's type or properties directly inside the switch for clearer and more concise code.Syntax
The switch statement with pattern matching uses case labels to match patterns such as types, constants, or properties.
- Type pattern:
case Type variableName:matches if the value is of that type and assigns it to the variable. - Constant pattern:
case constantValue:matches if the value equals the constant. - Property pattern:
case Type { Property: value }:matches if the object has a property with a specific value.
The switch expression can also be used for concise pattern matching.
csharp
switch (obj) { case int i: // Matches if obj is int, assigns to i break; case string s when s.Length > 0: // Matches non-empty string break; case null: // Matches null break; default: // Matches anything else break; }
Example
This example shows how to use pattern matching in a switch statement to identify the type of an object and respond accordingly.
csharp
using System; class Program { static void Main() { object[] items = { 42, "hello", 3.14, null }; foreach (var item in items) { switch (item) { case int i: Console.WriteLine($"Integer: {i}"); break; case string s when s.Length > 0: Console.WriteLine($"Non-empty string: {s}"); break; case null: Console.WriteLine("Null value"); break; default: Console.WriteLine("Other type"); break; } } } }
Output
Integer: 42
Non-empty string: hello
Other type
Null value
Common Pitfalls
Common mistakes when using pattern matching in switch include:
- Forgetting the
whenkeyword for additional conditions. - Not handling
nullvalues explicitly, which can cause unexpected behavior. - Using patterns that overlap without ordering cases properly, causing unreachable code.
- Confusing
case Type variable:with simple type checks without variable assignment.
csharp
/* Wrong: missing 'when' keyword for condition */ switch (obj) { case string s s.Length > 0: // Syntax error Console.WriteLine(s); break; } /* Correct: use 'when' for condition */ switch (obj) { case string s when s.Length > 0: Console.WriteLine(s); break; }
Quick Reference
| Pattern Type | Description | Example |
|---|---|---|
| Type pattern | Matches if value is a specific type and assigns it | case int i: |
| Constant pattern | Matches if value equals a constant | case 42: |
| Property pattern | Matches if object properties match values | case Point { X: 0, Y: 0 }: |
| Var pattern | Always matches and assigns value | case var x: |
| When clause | Adds extra condition to a pattern | case string s when s.Length > 0: |
Key Takeaways
Use
case Type variable: to match and extract values by type in switch.Add
when clauses for extra conditions on matched patterns.Always handle
null explicitly to avoid unexpected results.Order cases carefully to prevent unreachable code due to overlapping patterns.
Pattern matching makes switch statements clearer and more concise.