Read the syntax
A switch statement compares a value with case labels and executes the matching section; break exits that section. A default section handles values without a listed match.
A switch expression returns a value using pattern => result arms. The _ pattern is a fallback, and when guards add conditions beyond a simple equality match.
Syntax
switch (value) {\n case option:\n // code\n break;\n default:\n // code\n}Examples
Traditional switch Statement
The classic switch syntax, requiring break to prevent fall-through.
int day = 3;
string dayName;
switch (day)
{
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown";
break;
}
Console.WriteLine(dayName); // Wednesday- The classic switch syntax, requiring break to prevent fall-through.
- A switch statement compares a value with case labels and executes the matching section; break exits that section. A default section handles values without a listed match.
- day is 3, so the third case assigns Wednesday and break exits the switch. The later WriteLine prints the assigned name.
Grouping Cases
Multiple case labels can share the same code block by stacking them.
int month = 4;
string season;
switch (month)
{
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
default:
season = "Other";
break;
}
Console.WriteLine(season); // Spring- Multiple case labels can share the same code block by stacking them.
- A switch statement compares a value with case labels and executes the matching section; break exits that section. A default section handles values without a listed match.
- Cases 3, 4 and 5 share the Spring assignment. For month 4, that shared section runs and season prints Spring.
Modern switch Expression
Using arrow syntax to return a value directly - more concise, and no fall-through risk.
int day = 6;
string dayType = day switch
{
1 or 2 or 3 or 4 or 5 => "Weekday",
6 or 7 => "Weekend",
_ => "Invalid day"
};
Console.WriteLine(dayType); // Weekend- Using arrow syntax to return a value directly - more concise, and no fall-through risk.
- A switch statement compares a value with case labels and executes the matching section; break exits that section. A default section handles values without a listed match.
- The pattern 6 or 7 matches day 6, so the expression returns Weekend. The _ arm catches any unlisted day instead of leaving a value unset.
Pattern Matching in switch
Modern switch expressions can match on type and shape, not just exact values.
object item = 42;
string description = item switch
{
int n when n > 0 => $"Positive number: {n}",
int n when n < 0 => $"Negative number: {n}",
int => "Zero",
string s => $"A string: {s}",
null => "Nothing here",
_ => "Unknown type"
};
Console.WriteLine(description); // Positive number: 42- Modern switch expressions can match on type and shape, not just exact values.
- A switch statement compares a value with case labels and executes the matching section; break exits that section. A default section handles values without a listed match.
- The positive-int pattern binds 42 to n and its when guard succeeds. The resulting string is Positive number: 42; later arms are not considered.
Best practices
- Never forget break in a traditional switch statement - a missing break causes execution to fall through into the next case
- Prefer the modern switch expression (C# 8+) when your target version supports it - it removes the fall-through pitfall and is more concise
- Always include a default case (or _ in a switch expression) to handle unexpected values gracefully
- Use switch over a long else if chain when comparing one variable against several specific, discrete values or patterns
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference