By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
The switch statement and switch expressions in C# are powerful control flow constructs that allow you to execute one block of code among many options based on the value of a variable. Pattern matching enhances these constructs by enabling more complex conditions and type checks. Mastering these concepts is crucial for writing clean, efficient, and maintainable code. Incorrect usage can lead to bugs, reduced performance, and harder-to-read code. For example, misusing switch statements can result in unintended fall-through behavior, leading to logical errors in your application.
csharp int number = 2; switch (number) { case 1: Console.WriteLine("One"); break; case 2: Console.WriteLine("Two"); break; default: Console.WriteLine("Other"); break; }
⚠️ Pitfall: Forgetting the break statement causes fall-through, executing subsequent cases.
break
Introduce Switch Expressions
csharp string message = number switch { 1 => "One", 2 => "Two", _ => "Other" }; Console.WriteLine(message);
⚠️ Pitfall: Misusing the discard pattern _ can lead to unintended default behavior.
_
Implement Pattern Matching
csharp object obj = "Hello"; switch (obj) { case string s: Console.WriteLine($"String: {s}"); break; case int i: Console.WriteLine($"Integer: {i}"); break; default: Console.WriteLine("Unknown type"); break; }
⚠️ Pitfall: Overlooking type safety can lead to runtime errors.
Add Guard Clauses
csharp switch (obj) { case string s when s.Length > 5: Console.WriteLine($"Long string: {s}"); break; case string s: Console.WriteLine($"Short string: {s}"); break; default: Console.WriteLine("Unknown type"); break; }
Experts view switch statements and expressions as tools for organizing and simplifying decision-making logic. They focus on readability and maintainability, using pattern matching to handle complex conditions elegantly. Instead of writing nested if-else statements, they leverage the expressive power of switch constructs to make their code more intuitive and less error-prone.
Exam trap: Questions that test for unintended fall-through behavior.
The mistake: Using the discard pattern _ incorrectly in switch expressions.
Exam trap: Scenarios where _ is misused, leading to incorrect outputs.
The mistake: Overlooking type safety in pattern matching.
Exam trap: Questions that involve type-specific operations without type checks.
The mistake: Incorrect guard clause logic.
Scenario: You need to determine the day of the week based on an integer input.Question: Write a switch expression to return the day of the week.Solution:
int dayNumber = 3; string day = dayNumber switch { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Invalid day" }; Console.WriteLine(day);
Answer: "Wednesday" Why it works: The switch expression maps the integer to the corresponding day of the week.
Scenario: You need to handle different types of objects in a collection.Question: Write a switch statement with pattern matching to handle strings and integers differently.Solution:
object item = "Hello"; switch (item) { case string s: Console.WriteLine($"String: {s}"); break; case int i: Console.WriteLine($"Integer: {i}"); break; default: Console.WriteLine("Unknown type"); break; }
Answer: "String: Hello" Why it works: Pattern matching identifies the type of the object and executes the corresponding case.
Scenario: You need to classify a string based on its length.Question: Write a switch statement with guard clauses to classify the string.Solution:
string text = "CSharp"; switch (text) { case string s when s.Length > 5: Console.WriteLine($"Long string: {s}"); break; case string s: Console.WriteLine($"Short string: {s}"); break; default: Console.WriteLine("Unknown type"); break; }
Answer: "Short string: CSharp" Why it works: Guard clauses refine the pattern matching based on the string's length.
switch (variable) { case value: action; break; default: action; }
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.