Skip to content
Dave Dunkin edited this page Oct 31, 2022 · 2 revisions

Do not use a switch expression on a constant value

Avoid patterns such as:

var result = true switch
{
    _ when someCondition => result1,
    _ when someOtherCondition => result2,
    _ => throw new InvalidOperationException(), // someCondition and someOtherCondition are actually mutually exclusive
}

Instead:

var result = someCondition ? result1 : result2;
Clone this wiki locally