views:

551

answers:

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

A: 

move the string declaration to before the

switch(value)

statement. Then assign x for each case.

Pat
He's not asking HOW to get it to work... but why there is no compiler error.
JTA
+7  A: 

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

Lars A. Brekken
Thank you - I did not find the other question.
Jeremy Cron
+12  A: 

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.

Noldorin
please correct case 2, then your code would be fine.
Syed Tayyab Ali
@Syed: Yeah, I meant to point out that for the scoped switch block the compiler *would* generate an error. I've clarified that to show that you need to redefine x in this case.
Noldorin
@Noldorin: Another thing I want to let you know, that his code was not getting any error in case 2. That is because he already defined string x before switch. That become global and case 2 always execute without any error.
Syed Tayyab Ali
@Syed - you are incorrect - I DID NOT define x outside of the switch.
Jeremy Cron
@JCron: If anything is bad, that came from my side. If anything good, that is because of Creator.Be cool and accept my humble apology.
Syed Tayyab Ali
@JCron: i m sorry.
Syed Tayyab Ali
A: 

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;
}
...
Syed Tayyab Ali
if you case contain multiple statements, then put {your multiple statements in brackets} and outside put break;
Syed Tayyab Ali
see case 2, you did not declare string X, but in my case 2, it is properly definded
Syed Tayyab Ali
whoever give vote down, please specify reason, so I know what is wrong with my reply..
Syed Tayyab Ali
-1 for comments and insisting that the code is incorrect.
Jeremy Cron
@JCron, if your code is correct, then why you are here on SO.My dear friend, we are here to helping you out.
Syed Tayyab Ali
most probably, you already defined string x, out side the switch statement, therefore you are not getting any error.
Syed Tayyab Ali
@Syed - Please read the question that I asked. I didn't ask 'Why is my correct code correct?' I didn't understand some code that I came across and wanted to. Something that I think is a perfectly reasonable question to ask.
Jeremy Cron
@Syed - I don't understand why you don't understand that I didn't declare string x outside of the switch. read the accepted answer.
Jeremy Cron
@JCron: My dear frind, you are still not reading accepted answer carefully, he also mentioned that you need to re-declare x in your scope.
Syed Tayyab Ali
@Syed - you aren't understanding the question OR the answer. If the braces are used, x needs redefined, else the code compiles and works as I have it in my question. Don't believe me? Try it out before posting any more incorrect comments.
Jeremy Cron
just a quick comment: if you dont use braces, then cases become ambiguous and act weird.
Syed Tayyab Ali
+2  A: 

Is there something that I am not understanding about the switch statement in C#?

Seems likely!

Why would this not be an error when case 2 is used?

It's not an error presumably because its a legal C# program that follows the rules laid down in the C# specification.

Perhaps a more fruitful way to approach the question is for you to explain precisely why you believe this should be an error. Preferably by referring to the section of the specification which you believe has been violated.

Eric Lippert
-1, this should've been a comment, not an answer to my question.
Jeremy Cron
+4  A: 

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.

Reed Copsey
+1  A: 

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.

RedFilter