...but it doesn't.
Actually, it does. The reason that you get a compiler error on the code
{
string foo = "foo";
}
string foo = "new foo!";
is because local variables are in scope throughout the entire block in which the declaration occurs. Therefore, the "second" declaration of foo
is in scope in the whole block, including "above" where it is declared, and therefore it conflicts with the "first" foo
declared in the inner scope.
You could do this:
{
string foo = "foo";
}
{
string foo = "new foo!";
}
Now, the two scopes do not overlap and you do not get the compiler error that you are implicitly referring to in saying "...but it doesn't."
Are there any benefits, features or uses of why you would want to do this?
It lets you use the same simple name in two different blocks of code. I think this is in general a very bad idea, but that is what this feature lets you do.