For C #, ISO 23270 (Information Technology - Programming Languages โโ- C #), & sect; 10.3 (Declaration):
Each block, switch block, for-statement, foreach-statement or using-statement creates a declaration space for local variables and local constants called a declaration space of a local variable . Names entered into this declaration space through local declaration variables and local constants.
If the block is the instance body of a constructor, method or statement, or get or set accessor to declare an indexer, the parameters declared in such an declaration are members of the local space of the declaration of variable blocks.
If the block is the body of a general method, the type parameters declared in such a declaration are members of the local declaration space of variable blocks.
This is an error for two members of the local space of variable variables with the same name. This is a mistake for the local variable declaration space and nested local variables the variable declaration space to contain elements with the same name.
[Note: Thus, it is not possible to declare a local variable or constant with the same name as a local variable or constant in a closed block inside a nested block. It is possible that two nested blocks contain elements of the same name if neither of the blocks contains the other. end note]
So,
public void foobar() { if ( foo() ) { int i = 0 ; ... } if ( bar() ) { int i = 0 ; ... } return ; }
is legal but
public void foobar() { int i = 0 ; if ( foo() ) { int i = 0 ; ... } ... return ; }
not legal. Personally, I find this limitation quite annoying. I see how the compiler warning about overlapping areas, but a compilation error? Too many belts and suspenders, IMHO. I could see the virtue of the compiler and / or pragma option, though (perhaps -pedantic / -practical , #pragma pedantic vs #pragma practical , B^) ).
Nicholas Carey May 27 '11 at 19:13 2011-05-27 19:13
source share