Given this code:
List<string> things = new List<string>();
foreach (string thing in things)
{
string foo = thing.ToUpper();
}
string foo = String.Empty;
Why does the compiler complain that foo is declared twice? Surely the instance declared in the foreach loop is only valid within the scope of the loop?
While you can only refer to the outer
fooafter you declared it, locals are allocated at the beginning of a function which means the innerfoowill overshadow the outer one, even if it hasn’t been declared yet.