C# TutorialLesson 16Free preview
C# Switch
This lesson is part of C# · All levels
Summary
The switch is suitable for choosing actions based on a specific value, such as menu or day of the week. Each case ends at a break so that the execution does not fall to the next case. Modern C# also provides a concise switch expression to return the value of a mapping.
Key points
- Switch checks one expression and then runs the case blocks that match the value.
- Each case is generally ended with a break; without a break, the execution falls to the next case block.
- The default is executed when there is no matching case, similar to else.
- Several values can be used in a single block case by separating the values using the colon.
- Switch expression with arrow returns value and is suitable for simple mapping.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.