C# Foundations · 8 min read · Beginner
Control Flow & Pattern Matching
Write expressive C# switch expressions with relational patterns (>= 90), discards (_), and foreach loops.
What you'll learn
- Use C# switch expressions with => arms and the _ discard default
- Match numeric ranges directly using relational patterns (>=, <, and)
- Iterate cleanly over collections with foreach
C#‘s pattern matching turns nested if / else if chains into concise, readable expressions. You can match exact values, types, properties, and numeric ranges (>= 90, >= 80 and < 90) directly inside a switch expression.
Fill-in-the-Blank Exercise
C# Switch Expression
Return 'Pass' for scores >= 80 and 'Retry' for all other scores.
string status = score
{
>= 80 => "Pass",
=> "Retry"
};Interactive Code Lab
C#PatternMatching.cs
Expected OutputClick “Run Live” to compile with Mono C#
96: Distinction 84: Pass 70: Review
Check Your Understanding
Score: 0 / 1
1. In a C# switch expression, what does the underscore (_) pattern match?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.