for (int i = 0; i < 10; i++)
{
Foo();
}
int i = 10; // error, \'i\' already exists
----------------------------------------
for (int i = 0; i < 10; i
It is because the declaration space defines i
at the method level. The variable i
is out of scope at the end of the loop, but you still can't redeclare i
, because i
was already defined in that method.
Scope vs Declaration Space:
http://csharpfeeds.com/post/11730/Whats_The_Difference_Part_Two_Scope_vs_Declaration_Space_vs_Lifetime.aspx
You'll want to take a look at Eric Lippert's answer (who by default is always right concerning questions like these).
http://blogs.msdn.com/ericlippert/archive/2009/08/03/what-s-the-difference-part-two-scope-vs-declaration-space-vs-lifetime.aspx
Here is a comment from eric on the above mentioned post that I think talks about why they did what they did:
Look at it this way. It should always be legal to move the declaration of a variable UP in the source code so long as you keep it in the same block, right? If we did it the way you suggest, then that would sometimes be legal and sometimes be illegal! But the thing we really want to avoid is what happens in C++ -- in C++, sometimes moving a variable declaration up actually changes the bindings of other simple names!