Variable declaration in a C# switch statement

C#Switch Statement

C# Problem Overview


Why is it that in a C# switch statement, for a variable used in multiple cases, you only declare it in the first case?

For example, the following throws the error "A local variable named 'variable' is already defined in this scope".

switch (Type)
{
	case Type.A:
        	string variable = "x";
              	break;
	case Type.B:
        	string variable = "y";
              	break;
}

However, per the logic, the initial declaration should not be hit if the type is Type.B. Do all variables within a switch statement exist in a single scope, and are they created/allocated before any logic is processed?

C# Solutions


Solution 1 - C#

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;
}

Solution 2 - C#

I believe it has to do with the overall scope of the variable, it is a block level scope that is defined at the switch level.

Personally if you are setting a value to something inside a switch in your example for it to really be of any benefit, you would want to declare it outside the switch anyway.

Solution 3 - C#

Yes, the scope is the entire switch block - unfortunately, IMO. You can always add braces within a single case, however, to create a smaller scope. As for whether they're created/allocated - the stack frame has enough space for all the local variables in a method (leaving aside the complexities of captured variables). It's not like that space is allocated during the method's execution.

Solution 4 - C#

Because their scope is at the switch block. The C# Language Specification states the following:

> The scope of a local variable or constant declared in a switch block is the switch block.

Solution 5 - C#

The variables do share scope in the C# compiler. However, scope doesn't exist in the same way in CIL. As for actual creation / initialization... the .NET memory model lets the compiler move reads / writes a bit as long as simple rules are followed unless the variable is marked as volatile.

Solution 6 - C#

switch is a really primitive procedural implementation that has been around since the ages of C itself (even before C++).

The whole switch is a block that serves as a scope-contained GOTO: (hence the : in each case). If you took some assembler classes, that might seem familiar.

That is why switch use is most helpful when combining with Enums and not using break in every single case like

switch(mood)
{
    case Mood.BORED:
    case Mood.HAPPY:
        drink(oBeer) // will drink if bored OR happy
break;

    case Mood.SAD: // unnecessary but proofs a concept
    default:
        drink(oCoffee)
break;
}

Solution 7 - C#

The initialization takes place in the case, but the declaration is effectively done at the top of the scope. (Psuedo-code)

switch (Type)
{
string variable;

    case Type.A:
            variable = "x";
                break;
    case Type.B:
            variable = "y";
                break;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremyView Question on Stackoverflow
Solution 1 - C#Michael BurrView Answer on Stackoverflow
Solution 2 - C#Mitchel SellersView Answer on Stackoverflow
Solution 3 - C#Jon SkeetView Answer on Stackoverflow
Solution 4 - C#itsmattView Answer on Stackoverflow
Solution 5 - C#jezellView Answer on Stackoverflow
Solution 6 - C#percebusView Answer on Stackoverflow
Solution 7 - C#James CurranView Answer on Stackoverflow