Block Level Statement Block Level Statement in C #

Is there a reason why I am missing the fact that a block inside a case statement is not considered a block level declaration space?

I keep getting an error (variable already declared) when trying

case x: var someVariable = 42; break; case y: var someVariable = 40; break; 

but i can do

 case x: try{var someVariable = 42;}catch{} break; case y: try{var someVariable = 40;}catch{} break; 

If C # allows a fall through operators, it makes sense, but it doesnโ€™t, and I canโ€™t come up with a scenario where you can declare a variable in a case case and use it outside this block.

+7
c # language-design switch-statement
Jul 02 '09 at 14:10
source share
5 answers

UPDATE: this question was used as inspiration for this blog; see additional information.

http://ericlippert.com/2009/08/13/four-switch-oddities/

Thanks for the interesting question.




The various answers contain several misunderstandings and incorrect statements, and none of them explains why this is illegal. I will try to be final.

First of all, to be strictly correct, โ€œareaโ€ is the wrong word to describe a problem. Coincidentally, last week I wrote a blog post about this exact misuse of the โ€œsphereโ€; which will be published after my series on iterator blocks, which will work during July.

The correct term to use is the "declaration space". A declaration space is an area of โ€‹โ€‹code in which two different things cannot be declared the same. The script described here is a symptom of the fact that the switch section does not define the declaration space, although the switch block does. Since two OP declarations are in the same declaration space and have the same name, they are illegal.

(Yes, the switch block also determines the scope, but this fact is not relevant to the question, since the question is about the legitimacy of the declaration , and not the semantics of identifier search .)

A reasonable question: "Why is it not legal?" The reasonable answer is "okay, why should it be"? You can do this in one of two ways. Or it is legal:

 switch(y) { case 1: int x = 123; ... break; case 2: int x = 456; ... break; } 

or is it legal:

 switch(y) { case 1: int x = 123; ... break; case 2: x = 456; ... break; } 

but you cannot have it in both directions. C # designers chose the second way, which seems to be a more natural way to do this.

This decision was made on July 7, 1999, just shyly ten years ago. The comments in the notes from this day are extremely brief, simply stating: โ€œThe case with the switch does not create its own ad space,โ€ and then gives some example code that shows what works and what doesn't.

To learn more about what was in the minds of designers on that particular day, I would have to offend people a lot about what they thought ten years ago and let them know what is ultimately a trivial problem; I'm not going to do that.

In short, there is no particularly good reason to choose one of the ways; both have virtues. The language design team chose one of the ways because they needed to choose one; the one they chose seems reasonable to me.

+29
Jul 02 '09 at 20:55
source share

A - you did not fail, but you can use goto to move to another marked block. Therefore, the blocks must be within the same volume.

+8
Jul 02 '09 at 14:16
source share

You can also do:

 case x: {var someVariable = 42;} break; case y: {var someVariable = 40;} break; 

Essentially, curly braces create a lexical region, so without curly braces, someVariable is loaded into the character table twice. I believe that this choice was most likely made simply to avoid confusion and possibly avoid difficulties in constructing the symbol table.

+3
Jul 02 '09 at 14:12
source share

Because cases are not blocks, braces do not indicate scope. Cases due to lack of a better word, like shortcuts.

You better declare a variable outside the switch() and use it after that. Of course, in this case you cannot use the var keyword because the compiler will not know which type to initialize.

+2
Jul 02 '09 at 14:13
source share

You can simply declare a variable outside the scope of the switch statement.

 var someVariable; switch(); case x: someVariable = 42; break; case y: someVariable = 40; break; 
0
Jul 02 '09 at 14:16
source share



All Articles