C# Switch
When one value can lead to many different outcomes, a long if/else if chain becomes hard to read. C# offers two tools for this: the classic switch statement and the modern, expression-based switch expression.
The switch statement
A switch compares a value against a list of case labels and runs the matching section. Every section must end with break (or another jump such as return) — C# does not let execution "fall through" into the next case by accident.
using System;
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("Some other day");
break;
}Wednesday
The optional default section runs when no case matches.
Several labels for one section
Stack case labels to share the same code:
using System;
string[] commands = { "start", "go", "stop", "pause" };
foreach (string cmd in commands)
{
switch (cmd)
{
case "start":
case "go":
Console.WriteLine(quot;{cmd}: running");
break;
case "stop":
Console.WriteLine(quot;{cmd}: stopped");
break;
default:
Console.WriteLine(quot;{cmd}: unknown command");
break;
}
}start: running
go: running
stop: stopped
pause: unknown command
Patterns and when clauses
Case labels can be patterns, not just constants. Relational patterns (< 0), type patterns (int n) and when guards let a switch handle ranges and types:
using System;
object[] values = { 7, -2, "hi", 3.5, null };
foreach (object v in values)
{
switch (v)
{
case int n when n < 0:
Console.WriteLine(quot;negative int {n}");
break;
case int n:
Console.WriteLine(quot;int {n}");
break;
case string s:
Console.WriteLine(quot;string of length {s.Length}");
break;
case null:
Console.WriteLine("null");
break;
default:
Console.WriteLine(quot;something else: {v}");
break;
}
}int 7
negative int -2
string of length 2
something else: 3.5
null
Switch expressions
Often you just want to turn one value into another. A switch expression (C# 8+) does this compactly: the value comes first, then switch, then comma-separated pattern => result arms. The discard _ matches anything, like default.
using System;
static string Season(int month) => month switch
{
12 or 1 or 2 => "Winter",
>= 3 and <= 5 => "Spring",
>= 6 and <= 8 => "Summer",
>= 9 and <= 11 => "Autumn",
_ => "Invalid month"
};
Console.WriteLine(Season(1));
Console.WriteLine(Season(4));
Console.WriteLine(Season(10));
Console.WriteLine(Season(13));Winter
Spring
Autumn
Invalid month
Switch expressions return a value, so they can be used anywhere an expression is allowed: on the right side of =, as an argument, or as a method body.
Tuple and property patterns
Switch on several values at once by combining them into a tuple, or match on an object's properties:
using System;
static string Play(string a, string b) => (a, b) switch
{
_ when a == b => "Draw",
("rock", "scissors") or ("scissors", "paper") or ("paper", "rock") => "Player 1 wins",
_ => "Player 2 wins"
};
Console.WriteLine(Play("rock", "scissors"));
Console.WriteLine(Play("paper", "paper"));
Console.WriteLine(Play("paper", "scissors"));
string text = "hello";
string size = text switch
{
{ Length: 0 } => "empty",
{ Length: < 5 } => "short",
_ => "long"
};
Console.WriteLine(size);Player 1 wins
Draw
Player 2 wins
long
Statement or expression?
switch statement |
switch expression |
|
|---|---|---|
| Produces a value | No — runs statements | Yes |
| Syntax | case X: … break; |
X => value, |
| Default | default: |
_ => |
| No match | Nothing happens | Throws SwitchExpressionException (the compiler warns you) |
Tip: Use a switch expression whenever every branch just computes a value. Use the statement form when branches perform several actions.
Test yourself
What happens if a switch statement section has no break?
- Execution falls through to the next case
- It is a compile error
- The default section runs
- The switch restarts
In a switch expression, what does the _ arm do?
- Matches only null
- Matches any value not handled earlier
- Ignores the result
- Throws an exception
What is 5 switch { < 3 => "low", < 8 => "mid", _ => "high" }?
- "low"
- "mid"
- "high"
- It does not compile