Welcome back, Architect! Your digital city is growing fast, and construction materials are flooding in — steel beams, concrete blocks, glass panels, timber planks. Each material needs to be routed to the correct department for inspection.
You could use a long chain of if/else if statements… but there's an elegant alternative: the switch statement. Think of it as a sorting machine — you feed it a value, and it finds the matching chute to send it down.
📦 The Classic Switch Statement
The traditional switch compares a single value against multiple case labels:
string material = "Concrete";
switch (material)
{
case "Steel":
Console.WriteLine("Route to: Structural Engineering");
break;
case "Concrete":
Console.WriteLine("Route to: Foundation Division");
break;
case "Glass":
Console.WriteLine("Route to: Facade Design");
break;
default:
Console.WriteLine("Route to: General Warehouse");
break;
}
🔑 Key Rules of switch
break is required at the end of each case in C#. Unlike C/C++, C# does NOT allow implicit fall-through between cases that contain code.
default handles any value that doesn't match a case — like the "miscellaneous" bin. It's optional but highly recommended.
You CAN stack multiple cases together (explicit fall-through) when they share the same logic:
The switch value can be: string, int, char, enum, bool, or any type that supports ==.
🪜 Stacking Cases (Shared Logic)
Sometimes two materials go to the same department. You can stack cases with no code between them:
string material = "Titanium";
switch (material)
{
case "Steel":
case "Titanium":
case "Iron":
Console.WriteLine("Route to: Metallurgy Lab");
break;
case "Wood":
case "Bamboo":
Console.WriteLine("Route to: Organic Materials");
break;
default:
Console.WriteLine("Route to: General Intake");
break;
}
✨ Switch Expressions (C# 8+) — The Modern Way
C# 8 introduced switch expressions — a cleaner, more concise syntax that returns a value. This is the modern architect's preferred tool:
string material = "Glass";
string department = material switch
{
"Steel" => "Structural Engineering",
"Concrete" => "Foundation Division",
"Glass" => "Facade Design",
"Wood" => "Interior Finishes",
_ => "General Warehouse" // _ is the discard (default)
};
Console.WriteLine($"Department: {department}");
⚡ Switch Expression vs Switch Statement
Switch statement: Executes code blocks. Uses case/break. Good for complex multi-line logic.
Switch expression: Returns a value. Uses => (lambda arrows). Terser. Ideal for mapping one value to another.
The _ in switch expressions is the discard pattern — it matches anything (like default).
Switch expressions throw a SwitchExpressionException if no arm matches and there's no discard _.
🔢 Switching on Integers and Enums
Switch works beautifully with numeric types and enums:
In C and C++, forgetting break causes "fall-through" — execution silently continues into the next case. This is a legendary source of bugs. C# designers said "never again" and made break mandatory for non-empty cases. Smart architects learn from the mistakes of the past!
🏗️ Your Mission
The material routing station needs a controller program. Given a material name, route it to the correct department and assign a priority level. The city depends on your sorting skills!
📋 Instructions
**Material Routing Station**
You have a variable `material` set to `"Steel"`. Write a program that:
1. **Print the material**: `"Material: Steel"`
2. **Route the material to a department** using a `switch` statement (classic or expression — your choice):
- `"Steel"` → `"Structural Engineering"`
- `"Concrete"` → `"Foundation Division"`
- `"Glass"` → `"Facade Design"`
- `"Wood"` → `"Interior Finishes"`
- Anything else → `"General Warehouse"`
- Print: `"Department: Structural Engineering"`
3. **Assign a priority** using a switch expression:
- `"Steel"` → `"High"`
- `"Concrete"` → `"High"`
- `"Glass"` → `"Medium"`
- `"Wood"` → `"Low"`
- Anything else → `"Standard"`
- Print: `"Priority: High"`
Output must match **exactly**.
For Step 2, use switch (material) with case "Steel": etc. Don't forget break after each case! For Step 3, try the modern syntax: string priority = material switch { "Steel" => "High", ... , _ => "Standard" };