Coding/Learn/csharp/switch and patterns
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnSwitch Statements & Modern Switch Expressions
9 / 20 0%
Lesson 09 of 20
5 mins read

Switch Statements & Modern Switch Expressions

PreviousNext Lesson
In Plain English

When testing a single variable against many fixed values, the switch statement provides clean, jump-table optimized execution. Modern C# also introduces concise switch expressions that return values directly.

The Mental Model

"A switch statement is like a multi-channel TV remote. You press button 4 (case 4), and the TV instantly switches directly to Channel 4 without cycling through channels 1, 2, and 3."

Deep Dive: How It Works

Traditional Switch: Uses case, break;, and default: blocks.

Modern Switch Expression (C# 8+): string title = rank switch { 1 => "Gold", 2 => "Silver", _ => "Bronze" }; (note the _ wildcard default).

Pattern Matching: You can match against ranges or types: case > 90 => "A", case >= 80 => "B".

Syntax Blueprint

// Modern Switch Expression
string role = id switch {
  1 => "Admin",
  2 => "Editor",
  _ => "Viewer"
};

Concise, expression-based pattern matching in modern C#.

PointCore Rules to Remember

Point star
The Break Keyword: Traditional switch statements require break; at the end of each case.
Point star
Switch Expressions: Allows assigning results directly into variables with => syntax.
Point star
The Underscore Wildcard (_): Acts as the default catch-all in switch expressions.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting break; in traditional switch statements

Why it happens: C# enforces "no fallthrough" rules and will refuse to compile if break is omitted.

How to fix: Ensure every traditional case ends with break; or return;.

Pro Tip: Prefer modern switch expressions (var res = val switch { ... };) for cleaner, functional code!

Live Interactive Example

Hit Run Code to see it live
Modern Switch Expression
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: The switch expression cleanly matches 200 and assigns the result directly.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Map a day code with switch

  • PointDeclare int day = 1;
  • PointWrite a switch statement or expression where 1 is "Monday" and default is "Other".
  • PointPrint the day name.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates