C# Feature β Switch expressions
Overview
Section titled βOverviewβSwitch expressions are concise and composable, leveraging pattern matching and exhaustiveness.
Introduced In
Section titled βIntroduced InβC# 8 (2019)
string ColorName(ConsoleColor c){ switch (c) { case ConsoleColor.Red: return "Red"; case ConsoleColor.Green: return "Green"; default: return "Other"; }}string ColorName(ConsoleColor c) => c switch{ ConsoleColor.Red => "Red", ConsoleColor.Green => "Green", _ => "Other"};Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Use
_discard for default; consider completeness with enums.