Recall & Review
beginner
What is a switch expression in modern C#?
A switch expression is a concise way to select a value based on a pattern match, introduced in C# 8.0. It returns a value directly instead of using statements.
Click to reveal answer
beginner
How does a switch expression differ from a traditional switch statement?
A switch expression returns a value and is used inline, while a switch statement executes code blocks and does not return a value directly.
Click to reveal answer
beginner
What symbol is used to separate the input and the result in a switch expression case?
The '=>' symbol is used to separate the input pattern from the result expression in each case of a switch expression.
Click to reveal answer
intermediate
Can switch expressions in C# use pattern matching?
Yes, switch expressions support pattern matching, allowing you to match types, values, and conditions directly in the cases.
Click to reveal answer
intermediate
What happens if no case matches in a switch expression and no default case is provided?
If no case matches and there is no default case (using '_'), the switch expression throws a System.Runtime.CompilerServices.SwitchExpressionException at runtime.
Click to reveal answer
Which symbol is used to separate the pattern and result in a C# switch expression?
✗ Incorrect
The '=>' symbol separates the input pattern from the output expression in each case of a switch expression.
What does a switch expression return?
✗ Incorrect
Switch expressions return a value directly based on the matched case.
Which C# version introduced switch expressions?
✗ Incorrect
Switch expressions were introduced in C# 8.0.
What keyword is used as a default case in switch expressions?
✗ Incorrect
The underscore '_' is used as a discard pattern to represent the default case in switch expressions.
Can switch expressions use pattern matching with types?
✗ Incorrect
Switch expressions support pattern matching with types, values, and conditions.
Explain how a switch expression works in modern C# and give a simple example.
Think about how you choose an option and get a result in one line.
You got /4 concepts.
Describe the difference between a switch statement and a switch expression in C#.
One is like a list of instructions, the other is like a calculator.
You got /4 concepts.