0
0
C Sharp (C#)programming~5 mins

Switch expression (modern C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A=>
B:
C->
D=
What does a switch expression return?
AAn exception always
BA value based on the matched case
CA boolean indicating if a case matched
DNothing, it only executes code blocks
Which C# version introduced switch expressions?
AC# 7.0
BC# 6.0
CC# 9.0
DC# 8.0
What keyword is used as a default case in switch expressions?
A_
Bdefault
Celse
Dcase
Can switch expressions use pattern matching with types?
AOnly with strings
BNo
CYes
DOnly with integers
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.