Switch statements and expressions
A switch selects behavior based on a value or pattern. A switch statement is useful when cases perform actions; a switch expression is useful when cases calculate a value. Both can make a finite set of alternatives easier to read than a long chain of equality checks.
string category = "pickup";switch (category){ case "pickup": System.Console.WriteLine("Collect at desk"); break; case "local": System.Console.WriteLine("Deliver locally"); break; default: System.Console.WriteLine("Unsupported category"); break;}The pickup section runs and break exits the switch.
Expected output (reasoned, not executed):
Collect at deskSwitch statements
Write switch (category) followed by case labels and statements. A nonempty reachable case section cannot fall through into the next section in C#; normally end it with break. Several case labels may share one section. default handles unmatched input; without it, an unmatched value simply continues after the statement.
Switch expressions
A switch expression follows input switch { pattern => result, ... }. Each arm has a pattern and a result expression. The first matching arm whose guard, if present, is true supplies the value. _ is a discard pattern and can provide a final fallback. Nonexhaustive expressions may warn at compile time and throw at runtime when no arm matches.
Pattern basics and ordering
A constant pattern such as "local" matches a specific value. Relational patterns such as >= 10 compare a value to a constant; and combines patterns applied to the same value. Pattern syntax uses and and or, whereas ordinary bool expressions use && and ||. Put more specific patterns first; a later fully covered arm can be rejected as unreachable.
Examples and walkthroughs
string category = "local";int? fee = category switch{ "pickup" => 0, "local" => 5, "regional" => 9, _ => null};System.Console.WriteLine(fee?.ToString() ?? "Unsupported");The int? target supports a numeric fee or null for unsupported categories; zero remains a valid pickup fee.
Expected output (reasoned, not executed):
5int weight = 7;string size = weight switch{ <= 0 => "Invalid", <= 5 => "Small", > 5 and <= 10 => "Medium", _ => "Large"};System.Console.WriteLine(size);Seven matches the third arm. Nonpositive weights are handled before valid small weights.
Expected output (reasoned, not executed):
MediumCommon mistake and correction
An expression such as weight switch { _ => "Any", <= 5 => "Small" } puts a catch-all too early; the second arm is unreachable. Use a final fallback, and do not silently charge zero for unknown shipping categories.
Try it yourself
Calculate a nullable fee for "desk" (0), "near" (4), "far" (8) and every other value (null). Test each known category and an unknown one. Use the fallback text "Unsupported" only for null.
Next: For and while loops. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Switch statements and expressions — knowledge check
Apply the lesson concepts to the following scenarios.
What does break normally do inside a switch section?
Which form gives a value?
What does the _ arm match here?
What does > 5 and <= 10 match?
Why use int? for an unknown fee?