Another interesting discussion on MSDN Newsgroups:
The original question:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i / (list.Count) ); } int i = 23; IList list = new ArrayList(); Console.WriteLine( i / (list.Count) ); }I get the following errors :D:\Forecaster\TestForecaster\TestBaseEntities.cs(402): A local variablenamed 'i' cannot be declared in this scope because it would give a differentmeaning to 'i', which is already used in a 'child' scope to denote somethingelseD:\Forecaster\TestForecaster\TestBaseEntities.cs(402): The name 'i' does notexist in the class or namespace 'ForestResearch.UnitTests.TestBaseEntities'D:\Forecaster\TestForecaster\TestBaseEntities.cs(403): A local variablenamed 'list' cannot be declared in this scope because it would give adifferent meaning to 'list', which is already used in a 'child' scope todenote something elseD:\Forecaster\TestForecaster\TestBaseEntities.cs(403): The name 'list' doesnot exist in the class or namespace'ForestResearch.UnitTests.TestBaseEntities'The C# Language spec states : a.. The scope of a local variable declared in a for-initializer of a forstatement (§8.8.3) is the for-initializer, the for-condition, thefor-iterator, and the contained statement of the for statement.If the put the statements following the for statement in an anonymous blocklike this then the compiler is happy : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i / (list.Count) ); } { int i = 23; IList list = new ArrayList(); Console.WriteLine( i / (list.Count) ); } }Interestingly if I do the converse (i.e. put the for statement in ananonymous block and DON'T put the statements following in an anonymousblock) then I get the same compiler errors!I'm using Microsoft Visual Studio 2002.Questions :1. I would have thought that the scope of 'i' is the for initializer and forstatement so that this identifier could be used again as I have done abovein my first example ?2. Likewise I would have thought that the scope of the variable 'list'should just be the block in which it is declared and so could be used againas I have done in my first example ?3. Why does my second example produce no compiler errors ?Thanks in advance,
Really interesting because you would expect the compiler to work without a problem. After some basic research, I found out that it is a compiler feature designed to ensure that developers do not make a mistake while using variables in different scopes.
That is why when you put a curly brace around the code, it works fine - because then the compiler knows you are explicitly scoping your code into different execution scopes.
Funny though that the following code:
class CTest{ public void Foo() { int x = 4; Console.WriteLine(x); } int x = -1;}
compiles without an issue! I'm probably misusing the scope of a class-level variable and the compiler doesn't even bother protecting it now!
Strange!
Remember Me
Page rendered at Friday, November 21, 2008 2:29:36 AM (India Standard Time, UTC+05:30)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent the views of Microsoft Corporation in anyway.