Just to clarify, there are two rules violated here.
The first is that nested local variable declaration spaces may not contain two declarations of the same name.
The second is that nested local scopes may not contain two usages of the same simple name or declaration to mean two different things.
Both rules are violated. Note that the first rule is essentially a special case of the second rule. The second rule is more general; for example, this is also a violation:
class C
{
int x;
void M()
{
if (whatever)
{
string x = "";
}
x = 10;
}
}
Here the simple name x is used to mean this.x in one local scope and the local variable x in a nested scope. This is confusing; a simple name is required to mean the same thing throughout its entire block. We therefore make it illegal. This would be legal:
class C
{
int x;
void M()
{
if (whatever)
{
string x = "";
}
}
}
Even though the local variable is declared in a scope nested inside the scope of the field, this is allowed because the field's scope is not a local variable declaration space.
This is also legal:
class C
{
int x;
void M()
{
if (whatever)
{
string x = "";
}
else
{
x = 2;
}
}
}
because now the two conflicting usages of x are both used consistently throughout the entirity of their immediately containing scopes. Those scopes now do not overlap, so this is allowed. (It is still a bad idea however.)
These rules are there for your safety but they can be quite confusing. See
http://blogs.msdn.com/b/ericlippert/archive/tags/declaration+spaces/
and
http://blogs.msdn.com/b/ericlippert/archive/tags/scope/
for some articles on these language features.