DEV Community

Discussion on: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else`

Collapse
 
gabrielweidmann profile image
Gabriel Weidmann

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.