What is the purpose of the extra braces in Switch case?

C#Switch Statement

C# Problem Overview


I'm curious about this thing... see example:

switch(x)
{
    case(a):
        {
        //do stuff
        }
        break;
    case(b):
        //do stuff
        break;
}

All my life I've done it like case b, but since C# allows me to use it, and Visual Studio allows me to collapse that thing, I am curious - what is the real difference between case a (with braces) and case b?

C# Solutions


Solution 1 - C#

A pair of braces (not brackets -- [] -- and not parentheses -- () -- but braces {}) with zero or more statements in them is a legal statement in C#, and therefore may appear anywhere that a statement may legally appear.

As others have pointed out, the typical reason for doing so is because such a statement introduces a new local variable declaration space, which then defines the scope of the local variables declared within it. (Recall that the "scope" of an element is the region of program text in which the element may be referred to by its unqualified name.)

I note that this is particularly interesting in a switch statement because the scoping rules in a switch are a little bit strange. For details of how strange they are, see "Case 3:" in my article on the subject:

http://ericlippert.com/2009/08/13/four-switch-oddities/

Solution 2 - C#

Braces {} are used to define a scope for a set of operations. Bizarrely, the following will compile and work:

private void ConnectionStateChange(object sender, StateChangeEventArgs e)
{
    string s = "hi";
    switch(s)
    {
        case "hi":
            {
                int a = 1;
                a++;
            }
            {
                int a = 2;
                a++;
            }
            break;
    }

    {
        int a = 1;
        a++;
    }
    {
        int a = 2;
        a++;
    }
}

As you can see, in that one method I've created four variables, each called a. Each is entirely separate because, as local variables, they exist only within their own scope.

Does that make some sort of sense?

Solution 3 - C#

It creates a new scope in which you can create new variables.

Solution 4 - C#

It creates new scope for variables you used. Scope of variables can be tricky sometimes. For instance in the code you posted;

switch(x)
{
    case(a):
        {
        int i = 0;
        }
        break;
    case(b):
        i = 1; // Error: The name 'i' doesn't exist in the current context
        break;
}

The error makes sense here as in case(b) variable a is accessed out of scope. Now on the other hand,

switch(x)
{
    case(a):
        {
        int i = 0;
        }
        break;
    case(b):
        int i = 1; // Error: A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
        break;
}

Above two errors look contradictory to each other. To get around this you should define the scope separately in both case statements,

switch(x)
{
    case(a):
        {
        int i = 0;
        }
        break;
    case(b):
        {
        int i = 1; // No error
        }
        break;
}

Eric Lippert shared a very good link to his blog to explain variable scopes in case statement. You should have a look at it.

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
QuestionWishView Question on Stackoverflow
Solution 1 - C#Eric LippertView Answer on Stackoverflow
Solution 2 - C#IanView Answer on Stackoverflow
Solution 3 - C#Daniel A. WhiteView Answer on Stackoverflow
Solution 4 - C#ABHView Answer on Stackoverflow