简体   繁体   中英

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

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


Is there something that I am not understanding about the switch statement in C#? Why would this not be an error when case 2 is used?
Edit: This code works and doesn't throw an error.

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.

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

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.

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.

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

move the string declaration to before the

switch(value)

statement. Then assign x for each case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM