14

Possible Duplicate:
Variable declaration in c# switch statement

I've always wonderd :

when i write :

 switch (temp)
        {
            case "1":
                int tmpInt = 1;
                break;
           
        }

the case "1": region has a region of code which is executed ( until break)

now ,

a waterfall from above can't get into a case of 2 e.g. :

  switch (temp)
        {
            case "1":
                int tmpInt = 1;
             
            case "2":
             
                break;
        }

//error : break return is missing.

So i assume , they have different regions of executions ( case....break).

so why this errors appears ?

enter image description here

//conflict variable tmpInt is defined below.

p.s. this is just a silly question , still interesting.

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • because of some network issues, i am not able to see the image in your question.. can you provide its detail in text format. – Dr. Rajesh Rolen Jul 10 '12 at 08:51
  • 1
    I was also wandering why one can not name the variables the same if they are not in the same scope. Must be some reason for it... – Vale Jul 10 '12 at 08:53
  • the duplicated question - has chosen the wrong answer. the one with the higher score should be choosed. – Royi Namir Jul 10 '12 at 08:55
  • http://stackoverflow.com/questions/10095280/what-is-the-purpose-of-the-extra-braces-in-switch-case/10095322#10095322 – Habib Jul 10 '12 at 08:59
  • @Vale In this case, they *are* in the same scope. Hence the error. – Alex Jul 10 '12 at 09:00

5 Answers5

37

In C# the scope is determined solely by braces. If there are none, there is no separate scope. With switch/case there is obviously none. What you call »region of execution« has nothing to do at all with where you can refer to a variable. For a contrived example:

int x = 1;
goto foo;
// This part gets never executed but you can legally refer to x here.
foo:

You can do the following, though, if you like:

switch (temp)
{
    case "1":
        {
            int tmpint = 1;
            break;
        }
    case "2":
        {
            int tmpint = 1;
            break;
        }
}

In fact, for some switch statements I do that, because it makes life much easier by not polluting other cases. I miss Pascal sometimes ;-)

Regarding your attempted fallthrough, you have to make that explicit in C# with goto case "2".

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    +1 Wow, and I was renaming my variables every time... Thanks for the good tip. – Vale Jul 10 '12 at 09:03
  • 1
    +1 Great answer an really useful to someone (me) who was just reading around. :) – Stephen Murby Jul 10 '12 at 09:23
  • @Joey Aren't braces only needed around expressions within a Switch-Case which declare a new variable? Or is that just C++? – Nathan White Jul 10 '12 at 15:25
  • 1
    You can put blocks wherever you like and they always introduce a new scope. And yes, you don't have to use them with `switch`/`case` but you can and if you do you can have the same variable name in different `case`s. For the sake of consistency I'd use them in every `case` if I use it in one, though. – Joey Jul 10 '12 at 21:19
  • Why would you ever need to declare a new variable inside a case? How I understand this, declare the variable before the switch, then assign the value based on the switch condition. – Alexus Nov 24 '14 at 22:21
  • 1
    @Alexus: Not every case is as trivial as a single line. You are able to include a few lines more before the `break` ... – Joey Nov 25 '14 at 06:27
  • @Joey I understand that, but if you need to declare a new variable withing the switch, you are using it wrong. Switch is meant to alter the bahavior of your code mildly, if you need more complex changes to the behavior - move your code out to another method and call it. It will be more readable that way and you will avoid issues. But, I would still say that if you have a variable in some cases, not in others, you need to revise your design. – Alexus Nov 25 '14 at 18:39
2

Try this

int tmpInt = 0;
switch (temp)
        {
            case "1":
            case "2":
                tmpInt = 1;
                break;
        }

so when the case is 1 or 2 it will set tmpint to 1

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • this is not related. im not trying to put the same logic . just decalre same var name – Royi Namir Jul 10 '12 at 08:58
  • 1
    Why would you want the same variable if you can declare it above the Switch – JohnnBlade Jul 10 '12 at 09:00
  • 1
    Because it might not be of the same type. In the above example for the int, what you say is correct. However you could have a situation where, for example, you Deserialize a payload to a different type of payload in each case statement. So in this case you couldn't declare it above and would need it in braces. – caa Aug 02 '17 at 11:30
2

Section 8.5.1 of the C# language spec says:

The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. It is an error to refer to a local variable in a textual position that precedes the local-variable-declarator of the local variable. Within the scope of a local variable, it is a compile-time error to declare another local variable or constant with the same name.

The block in this case is the switch statement, as blocks are determined by braces.

Bruno Silva
  • 3,077
  • 18
  • 20
2

This happens because you're declaring a local variable with the same name in the same scope, just like intellisense tells you when you hover over the error line.

This is why you really should use curly brackets in each case:

switch(var)
{
    case 1: 
    {
        int temp=0;
    } break;
    case 2:
    {
        int temp=0;
    } break;
}

This fixes the "issue" (which really is not an issue, that's how scopes work).

Alex
  • 23,004
  • 4
  • 39
  • 73
-1

you are creating the same variable twice i.e. int tmpInt = 1;

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • 1
    this is the right answer - break does not define scope, it "breaks" you out of the current scope – AnthonyBlake Jul 10 '12 at 08:54
  • 1
    So shall I give -1 to the one hwo gave me -1 and to the op for not asking the correct question ? – HatSoft Jul 10 '12 at 08:56
  • 1
    @HatSoft `why this errors appears` in the pov of scope. i did asked that. the whole start of the question was about scopes an regions. – Royi Namir Jul 10 '12 at 08:57
  • Royi, HatSoft is absolutelly right. You are withing the same scope. And declaring the same variable in the same scope is the error you are having. – Alexus Nov 24 '14 at 22:26