Today I discovered a, presumably, "feature" of c#:
error CS0136: A local variable named
foo
cannot be declared in this scope because it would give a different meaning tofoo
, which is already used in achild
scope to denote something else
Which can simply be reproduced by trying to compile this snippet:
if (false) {
int foo = 42;
}
int foo = 1337;
Why can't I define foo
in the parent scope? The foo
from the child scope is not visible in the parent scope, nor would it be still allocated, it's therefore absolutely legal in any proper language. What did I miss? What is the reasoning behind this?