C# switch variable initialization: Why does this code NOT cause a compiler error or a runtime error?

后端 未结 6 1365
粉色の甜心
粉色の甜心 2020-12-17 23:46
...
case 1:
   string x = \"SomeString\";
   ...
   break;
case 2:
   x = \"SomeOtherString\";
   ...
   break;
...


Is there something that

相关标签:
6条回答
  • 2020-12-18 00:10

    if are creating any local variable within case, you can not use them out side case.

    ...
    int opt ;
    switch(opt)
    {
    case 1:
    {
       string x = "SomeString";
       ...
    }
       break;
    case 2:
    {
       string x = "SomeOtherString";
       ...
    }
       break;
    default:
    {
    //your code
    }
    break;
    }
    ...
    
    0 讨论(0)
  • 2020-12-18 00:12

    The documentation on MSDN says :

    The scope of a local variable declared in a switch-block of a switch statement (Section 8.7.2) is the switch-block.

    Also, a similar question has been asked before: Variable declaration in c# switch statement

    0 讨论(0)
  • 2020-12-18 00:15

    You have to be careful how you think about the switch statement here. There's no creation of variable scopes going on at all, in fact. Don't let the fact that just because the code within cases gets indented that it resides within a child scope.

    When a switch block gets compiled, the case labels are simply converted into labels, and the appropiate goto instruction is executed at the start of the switch statement depending on the switching expression. Indeed, you can manually use goto statements to create "fall-through" situations (which C# does directly support), as the MSDN page suggests.

    goto case 1;
    

    If you specifically wanted to create scopes for each case within the switch block, you could do the following.

    ...
    case 1:
    {
       string x = "SomeString";
       ...
       break;
    }
    case 2:
    {
       string x = "SomeOtherString";
       ...
       break;
    }
    ...
    

    This requires you to redeclare the variable x (else you will receive a compiler error). The method of scoping each (or at least some) can be quite useful in certain situations, and you will certainly see it in code from time to time.

    0 讨论(0)
  • 2020-12-18 00:24

    There is no compiler error because the switch statement does not create a new scope for variables.

    If you declare a variable inside of a switch, the variable is in the same scope as the code block surrounding the switch. To change this behavior, you would need to add {}:

    ...
    case 1:
        // Start a new variable scope 
        {
            string x = "SomeString";
            ...
        }
        break;
    case 2:
        {
            x = "SomeOtherString";
            ...
        }
        break;
    ...
    

    This will cause the compiler to complain. However, switch, on it's own, doesn't internally do this, so there is no error in your code.

    0 讨论(0)
  • 2020-12-18 00:25

    It looks like the scoping of variables is within the switch, not the case, probably because cases can be stacked. Notice if you try to reference x outside of the switch it will fail.

    0 讨论(0)
  • 2020-12-18 00:34

    move the string declaration to before the

    switch(value)
    

    statement. Then assign x for each case.

    0 讨论(0)
提交回复
热议问题