The switch expression checks the input against patterns in order and returns the matching result or a default.
Execution Sample
C Sharp (C#)
object input = 42;
string result = input switch
{
int i when i > 0 => "Positive integer",
int i when i < 0 => "Negative integer",
string s => $"String: {s}",
_ => "Unknown"
};
This code uses a switch expression to check the input's type and value, returning a matching string.
Execution Table
Step
Input Value
Pattern Checked
Condition Result
Branch Taken
Output
1
42
int i when i > 0
True
Yes
"Positive integer"
2
42
int i when i < 0
Skipped
No
3
42
string s
Skipped
No
4
42
_ (default)
Skipped
No
Exit
42
N/A
Matched at step 1
End
"Positive integer"
💡 Input 42 matches pattern 'int i when i > 0' at step 1, so switch returns "Positive integer" and stops.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
Final
input
42
42
42
42
42
result
null
"Positive integer"
"Positive integer"
"Positive integer"
"Positive integer"
Key Moments - 3 Insights
Why does the switch expression stop checking after the first pattern matches?
Because switch expressions return the result immediately when a pattern matches, as shown in execution_table step 1 where the first pattern matches and the rest are skipped.
What does the underscore (_) pattern mean in the switch expression?
The underscore (_) is the default pattern that matches anything not matched before, as seen in execution_table step 4, but it is skipped here because an earlier pattern matched.
How does the 'when' clause affect pattern matching?
The 'when' clause adds an extra condition to the pattern; the pattern only matches if the condition is true, like 'int i when i > 0' in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
A"Unknown"
B"Positive integer"
C"Negative integer"
D"String: 42"
💡 Hint
Check the Output column at step 1 in the execution_table.
At which step does the switch expression stop checking patterns?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
Look at the Branch Taken column and see where the first 'Yes' appears.
If input was "hello" (a string), which pattern would match?
Astring s
Bint i when i < 0
Cint i when i > 0
D_ (default)
💡 Hint
Refer to the patterns and their types in the execution_sample code.
Concept Snapshot
Switch expressions check an input against patterns in order.
Each pattern can have a condition with 'when'.
The first matching pattern returns its result immediately.
Use '_' as a default catch-all pattern.
Syntax: result = input switch { pattern1 => value1, pattern2 => value2, _ => defaultValue };
Full Transcript
This example shows how a switch expression in C# tests an input value against several patterns. The input 42 is checked first against 'int i when i > 0', which matches because 42 is a positive integer. The switch returns "Positive integer" immediately and stops checking other patterns. The underscore (_) pattern is a default that matches anything else but is skipped here because an earlier pattern matched. The 'when' clause adds extra conditions to patterns. This flow helps write clear, concise code for multiple conditions.