loading...

re: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else` VIEW POST

FULL DISCUSSION
 

In C# I mostly use switches together with enums. Why?

First because if you use the switch code snippet it will generate you a switch-case with all enum parameters, e.g.:

public enum Values
{
    A,
    B,
    C
}

static void Switch(Values value)
{
    switch (value)
    {
        case Values.A:
            break;
        case Values.B:
            break;
        case Values.C:
            break;
        default:
            break;
    }
}

Second if you add a new case it will automatically suggest to use a value of the enum and typecheck the cases, e.g. you couldn't use a string.

code of conduct - report abuse