What if you could replace long, messy decision code with a simple, elegant expression?
Why Switch expression (modern C#)? - Purpose & Use Cases
Imagine you have to decide what to do based on many different conditions, like choosing what to wear depending on the weather. Doing this by writing many if-else statements can get confusing and long.
Using many if-else or old switch statements makes your code bulky and hard to read. It's easy to make mistakes or forget a case, and changing the logic later becomes a headache.
The modern C# switch expression lets you write all these conditions in a clear, short, and neat way. It makes your code easier to read and less error-prone by handling all cases in one place.
string result;
switch (weather) {
case "sunny":
result = "Wear sunglasses";
break;
case "rainy":
result = "Take an umbrella";
break;
default:
result = "Wear normal clothes";
break;
}var result = weather switch {
"sunny" => "Wear sunglasses",
"rainy" => "Take an umbrella",
_ => "Wear normal clothes"
};You can write clear, concise decision-making code that's easy to update and understand at a glance.
Think about a weather app that shows advice based on the forecast. Using switch expressions, the app's code stays clean and easy to add new weather types.
Manual if-else chains are long and error-prone.
Switch expressions simplify complex decisions into neat, readable code.
They make your programs easier to maintain and extend.
Practice
_ symbol in a modern C# switch expression?Solution
Step 1: Understand the role of
The underscore_in switch expressions_is used as a discard pattern that matches any value not matched by previous cases.Step 2: Recognize it as the default case
This means it acts like a default or fallback case to handle all other inputs.Final Answer:
It acts as a default case to catch all unmatched values. -> Option CQuick Check:
_= default case [OK]
- Thinking '_' declares a variable
- Confusing '_' with case separator
- Assuming '_' ends the switch
Solution
Step 1: Identify correct switch expression syntax
In modern C#, switch expressions use the syntax:variable switch { pattern => result, ... }.Step 2: Compare options to syntax
var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; matches this syntax exactly. Options B, C, and D use incorrect syntax like parentheses or keywords not used in switch expressions.Final Answer:
var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; -> Option BQuick Check:
Correct switch expression syntax = var result = number switch { 2 => "Even", 4 => "Even", 1 => "Odd", 3 => "Odd" }; [OK]
- Using parentheses instead of braces
- Using 'case' keyword inside switch expression
- Using colons instead of =>
int day = 3;
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
_ => "Unknown"
};
Console.WriteLine(dayName);Solution
Step 1: Match the value of
The variabledayin the switch expressiondayis 3, so the switch expression looks for the case matching 3.Step 2: Find the corresponding string for case 3
The case 3 returns "Wednesday". Since it matches, the default case_is ignored.Final Answer:
Wednesday -> Option AQuick Check:
day=3 matches 3 => "Wednesday" [OK]
- Choosing default case instead of matching case
- Confusing case numbers with strings
- Ignoring the switch expression syntax
string result = value switch
{
1 => "One",
2 => "Two",
3 => "Three",
default => "Other"
};Solution
Step 1: Check syntax for default case in switch expressions
Modern C# switch expressions use_as the default case, not the keyworddefault.Step 2: Identify the incorrect use of 'default'
The code usesdefault => "Other", which is invalid syntax in switch expressions.Final Answer:
Using 'default' keyword instead of '_' for default case. -> Option AQuick Check:
Default case = '_' not 'default' [OK]
- Using 'default' instead of '_'
- Forgetting commas between cases
- Confusing switch statement syntax with switch expression
Solution
Step 1: Understand pattern matching syntax in switch expressions
Modern C# supports relational patterns like< 10and logical patterns likeandto combine conditions.Step 2: Check each option for correct range coverage
var size = number switch { < 10 => "Small", >= 10 and <= 20 => "Medium", _ => "Large" }; uses< 10for "Small", and>= 10 and <= 20for "Medium", which correctly covers 10 to 20 inclusive. The default_covers "Large".Final Answer:
var size = number switch { < 10 => "Small", >= 10 and <= 20 => "Medium", _ => "Large" }; -> Option DQuick Check:
Use relational and logical patterns for ranges [OK]
- Using invalid syntax like '10 <= number <= 20'
- Missing inclusive range boundaries
- Using overlapping or incorrect ranges
Ready to prove it?
Pass the test and this pattern moves unknown
