Fork me on GitHub
TypeName ConvertToSwitchAnalyzer
Check Id CC0019
Category Style
Severity Info

Cause

Multiple ‘if’ and ‘else if’ on the same variable can be replaced with a ‘switch’ on the variable

Note: This diagnostic trigger for 3 or more ‘case’ statements

Example

public async Task Foo(string s)
{
    if (s == "A")
    {
        // ..
    }
    else if (s == "B")
    {
        // ..
    }
    else if (s == "C")
    {
        // ..
    }
    else
    {
        // ..
    }
}

Code fix

A code fix will be presented to you that will transform the code:

public async Task Foo(string s)
{
    switch (s)
    {
        case "A":
            break;
        case "B":
            break;
        case "C":
            break;
        default:
            break;
    }
}

Code fix

Related rules

None

See also

TBD