Variable declaration in a C# switch statement [duplicate]

If you want a variable scoped to a particular case, simply enclose the case in its own block:

switch (Type)
{
    case Type.A:
    {
        string variable = "x";
        /* Do other stuff with variable */
    }
    break;

    case Type.B:
    {
        string variable = "y";
        /* Do other stuff with variable */
    }
    break;
}

Leave a Comment