Code inspection: Replace 'switch' statement with 'switch' expression
This inspection reports switch statements whose branches only compute a value or assign one, so they can be rewritten as a switch expression. This usually makes the code shorter and keeps the branching logic closer to the produced result.
Example
int result;
switch (kind)
{
case TokenKind.Number:
result = 1;
break;
case TokenKind.Identifier:
result = 2;
break;
default:
result = 0;
break;
}
int result = kind switch
{
TokenKind.Number => 1,
TokenKind.Identifier => 2,
_ => 0
};
Quick-fix
A switch expression removes repetitive case, assignment, and break boilerplate while preserving the same result.
30 March 2026