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

Jeremy Cron
  • 2,404
  • 3
  • 25
  • 30
  • you just need to put string x = "SomeOtherString"; then it will work. the, you can not use case 1 variable in case 2. out of scope error will occur. – Syed Tayyab Ali May 14 '09 at 16:05
  • make sure case 2 require code modification. – Syed Tayyab Ali May 14 '09 at 16:06
  • @Syed - that is not correct. NO error occurs with this code. – Jeremy Cron May 14 '09 at 16:07
  • that is because you already defined string x, out side the switch statement, therefore you are not getting any error. that string x is acting global in case 2. Let me know. – Syed Tayyab Ali May 14 '09 at 16:20
  • @Syed - Your comments have been incorrect. x is ONLY defined in case 1 in my code. – Jeremy Cron May 14 '09 at 16:26
  • (OK then, a comment) It's not an error because the program obeys the rules of C# as described in our published specification. Why do you believe that this fragment should be an error? What part of the specification do you believe has been violated? – Eric Lippert May 14 '09 at 16:45
  • @eric-lippert and @JCron: just wrrap case 1 with braces, then he will definitely get compile time error. – Syed Tayyab Ali May 14 '09 at 16:50
  • 1
    @eric - I assumed that case 2 was in a different scope than case 1. It seemed logical that when defining a variable inside a case statement, that it wouldn't be available outside of that case statement. – Jeremy Cron May 14 '09 at 17:03
  • 2
    Though that is the case for many textual regions -- for example, the statement portion of a for, foreach, using, switch, and so on -- it is explicitly not true of a switch section. The specification carefully defines "scope" and describes what regions introduce scope boundaries. See the spec if you want the details. – Eric Lippert May 14 '09 at 17:20

6 Answers6

18

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
  • 144,213
  • 56
  • 264
  • 302
  • @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 May 14 '09 at 16:18
  • @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 May 14 '09 at 16:22
  • @Syed - you are incorrect - I DID NOT define x outside of the switch. – Jeremy Cron May 14 '09 at 16:25
  • @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 May 14 '09 at 16:30
8

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

Community
  • 1
  • 1
Lars A. Brekken
  • 24,405
  • 3
  • 25
  • 27
4

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
  • 554,122
  • 78
  • 1,158
  • 1,373
1

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.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
0

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
  • 3,643
  • 7
  • 31
  • 36
  • if you case contain multiple statements, then put {your multiple statements in brackets} and outside put break; – Syed Tayyab Ali May 14 '09 at 15:59
  • see case 2, you did not declare string X, but in my case 2, it is properly definded – Syed Tayyab Ali May 14 '09 at 16:04
  • whoever give vote down, please specify reason, so I know what is wrong with my reply.. – Syed Tayyab Ali May 14 '09 at 16:12
  • @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 May 14 '09 at 16:16
  • most probably, you already defined string x, out side the switch statement, therefore you are not getting any error. – Syed Tayyab Ali May 14 '09 at 16:19
  • @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 May 14 '09 at 16:19
  • @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 May 14 '09 at 16:19
  • @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 May 14 '09 at 16:26
  • @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 May 14 '09 at 16:29
  • just a quick comment: if you dont use braces, then cases become ambiguous and act weird. – Syed Tayyab Ali May 14 '09 at 16:33
-2

move the string declaration to before the

switch(value)

statement. Then assign x for each case.

Pat
  • 5,263
  • 1
  • 36
  • 53