Code inspection: Replace ternary expression with 'switch' expression
This inspection reports nested conditional expressions that repeatedly test the same input and can be rewritten as a switch expression. It usually appears when a chain of ?: conditions behaves like a compact multi-branch selection.
Example
var score =
status == OrderStatus.New ? 0 :
status == OrderStatus.Paid ? 1 :
status == OrderStatus.Shipped ? 2 :
3;
var score = status switch
{
OrderStatus.New => 0,
OrderStatus.Paid => 1,
OrderStatus.Shipped => 2,
_ => 3
};
Quick-fix
A switch expression is easier to scan than a long ternary chain and makes each case explicit.
30 March 2026