Multi-variable switch statement in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# 8.0 introduced tuple patterns and property patterns in switch expressions, enabling multi-variable switching in a single statement. Before C# 8, switch only matched a single value. Now you can switch on tuples like (x, y), combining multiple variables into one pattern match. This eliminates nested if/else chains and makes complex conditional logic readable and concise.
Traditional Single-Variable Switch
Tuple Pattern Switch (C# 8+)
Switch on multiple variables by wrapping them in a tuple:
The _ discard matches any value. Patterns are evaluated top to bottom — the first match wins.
Switch Expression with Guards (when)
Add conditions to patterns using when:
Property Pattern Matching
Match against object properties:
Nested Patterns
Switch Statement (Not Expression)
The switch statement form uses case with tuple patterns:
Three or More Variables
Tuples support any number of elements:
Replacing Nested If/Else
Before (nested ifs):
After (tuple switch):
Common Pitfalls
- Unreachable patterns: Patterns are evaluated top to bottom. A broader pattern above a specific one makes the specific one unreachable. The compiler warns about this — always place specific patterns before general ones.
- Missing exhaustiveness: Switch expressions must be exhaustive — all possible input combinations must be handled. Omitting the
_default when not all cases are covered produces a compiler warning. At runtime, an unmatched input throwsSwitchExpressionException. - C# version requirement: Tuple patterns require C# 8.0+, property patterns require C# 8.0+, and relational patterns (
> 0,< 10) require C# 9.0+. Verify your project'sLangVersionin.csproj. - Mutable tuple values: If the switched variables change between evaluation, the result is based on values at the time of the switch, not when the result is used. Capture values in local variables first if mutability is a concern.
- Overusing switch expressions: For simple two-way conditions,
if/elseis more readable. Switch expressions shine when there are three or more combined conditions — not for simple boolean checks.
Summary
- Use tuple patterns
(x, y) switch { ... }to match multiple variables in a single switch expression (C# 8+) - Combine with relational patterns (
> 0,< 10) andwhenguards for complex conditions (C# 9+) - Property patterns match against object properties without destructuring
- Place specific patterns before general ones — first match wins
- Switch expressions must be exhaustive; always include a
_default case
Related reading
- Multiline editing in Visual Studio Code
- Multiline string literal in C#
- Multiple actions were found that match the request in Web Api
- Multiple awaits vs Task.WaitAll - equivalent?
- Multiple DbContext in .NET Aspire
- Multiple file-extensions searchPattern for System.IO.Directory.GetFiles
- MultipleActiveResultSetsTrue or multiple connections?
- Multiply TimeSpan in .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.