I have been using C# for quite a long time but never realised the following:
public static void Main()
{
for (int i = 0; i < 5; i++)
{
}
int i = 4; //cannot declare as 'i' is declared in child scope
int A = i; //cannot assign as 'i' does not exist in this context
}
So why can I not use the value of 'i' outside of the for block if it does not allow me to declare a variable with this name?
I thought that the iterator variable used by a for-loop is valid only in its scope.
The easiest way to think about this is to move the outer declaration of I to above the loop. It should become obvious then.
It's the same scope either way, therefore can't be done.
There is a way of declaring and using
i
inside the method after the loop:You can do this in Java (it might originate from C I'm not sure). It is of course a bit messy for the sake of a variable name.
Kommer's answer is technically correct. Let me paraphrase it with a vivid blind-screen metaphor.
There is a one way blind screen between the for-block and the enclosing outer block such that the code from within the for-block can see the outer code but the code in the outer block cannot see the code inside.
Since the outer code cannot see inside , it cannot use anything declared inside. But since the code in the for-block can see both inside and outside , a variable declared at both places cannot be used unambiguously by name.
So either you don't see it , or you C# !
The reason you are not allowed to define a variable with the same name in both the for-loop as well as outside the for-loop is because variables in the outer-scope are valid in the inner-scope. Meaning that there would be two 'i' variables within the for-loop if this was allowed.
See: MSDN Scopes
Specifically:
and
And also: Local variable declarations (Section 8.5.1 of the C# specification)
Specifically:
(Emphasis mine.)
Which means that the scope of the
i
inside your for-loop, is the for-loop. Whereas the scope of thei
outside of your for-loop is the entire main method plus the for-loop. Meaning you'd have two occurrences ofi
inside the loop which is invalid according to the above.The reason why you're not allowed to do
int A = i;
is becauseint i
is only scoped for use within thefor
loop. Thus it is no longer accessible outside of thefor
loop.As you can see both of these issues are a result of scoping; the first issue (
int i = 4;
) would result in twoi
variables within thefor
loop scope. Whereasint A = i;
would result in access to a variable that is out of scope.What you could do instead is declare
i
to be scoped to the entire method, and then use it in both the method as well as the for-loop scope. This will avoid breaking either rule.EDIT:
The C# compiler could of course be changed to allow this code to compile quite validly. After all this is valid:
But would it really be beneficial to your code readability and maintainability to be able to write code such as:
Think about the potential for mistakes here, does the last
i
print out 0 or 4? Now this is a very small example, one which is quite easy to follow and track but it is definitely a lot less maintainable and readable than having declared the outeri
by a different name.N.B:
Please note, C#'s scoping rules differ from C++'s scoping rules. In C++ variables are only in scope from where they are declared until the end of the block. Which would make your code a valid construct in C++.
J.Kommer's answer is correct: briefly, it is illegal for a local variable to be declared in a local variable declaration space that overlaps another local variable declaration space that has a local of the same name.
There is an additional rule of C# that is violated here as well. The additional rule is that it is illegal for a simple name to be used to refer to two different entities inside two different overlapping local variable declaration spaces. So not only is your example illegal, this is illegal too:
Because now the simple name "x" has been used inside the local variable declaration space of "y" to mean two different things -- "this.x" and the local "x".
See http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/ for more analysis of these issues.
In addition to J.Kommer's answer (+1 btw). There's this in the standard for NET scope:
Thus the int i decalared within the for loop header will be in scope only during the for loop block, BUT it's lifetime lasts until the
Main()
code completes.