Hi-
I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:
for (int i=0; i<10; i++)
{
/////
}
int i = 100;
The compiler complains:
A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a
'child' scope to denote something else
This doesn't make sense. The variable i defined in the for loop
should go out of scope after the closing curly brace, and it should be
OK to define a new variable which just happens to have the same name,
right?
If i is still defined after the for block, this should work fine:
for (int i=0; i<10; i++)
{
/////
}
i = 100;
But this doesn't work either:
The name 'i' does ot exist in the class or namespace 'Dooku.kudu.bobo'
So it looks to me like i is neither in scope or out of scope after the
for loop. It's in limbo!
Any ideas?
Thanks,
Diane.
P.S. THis is not just a quirk of the for loop. THe behavior is the
same with conditionals and just plain old {} blocks...