Based on this recent question, I don’t understand the answer provided. Seems like you should be able to do something like this, since their scopes do not overlap
static void Main()
{
{
int i;
}
int i;
}
This code fails to compile with the following error:
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
I don’t think any of the answers so far have quite got the crucial line from the spec.
From section 8.5.1:
(Emphasis mine.)
In other words, the scope for the “later” variable includes the part of the block before the declaration – i.e. it includes the “inner” block containing the “earlier” variable.
You can’t refer to the later variable in a place earlier than its declaration – but it’s still in scope.