All rules
IDE0066Expression-level preferences MS default: Suggestion

Use switch expression

Prefer a `switch` expression over a `switch` statement where applicable.

Microsoft docs

Description

Suggests converting a switch statement that simply assigns or returns a value into a more concise switch expression.

Why it matters

Switch expressions are more concise and force exhaustiveness, reducing the chance of a forgotten case.

Examples

Avoid
switch (status)
{
    case Status.Open: return "open";
    default: return "closed";
}
Prefer
return status switch
{
    Status.Open => "open",
    _ => "closed",
};

Configurable options

Vote for the value each option should take in the generated .editorconfig.

csharp_style_prefer_switch_expression
default: true
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0