Why does local variable inside foreach loop conflict with variable declared outside the loop?

前端 未结 1 605
孤街浪徒
孤街浪徒 2021-01-20 09:47

Given this code:

List things = new List();

foreach (string thing in things)
{
    string foo = thing.ToUpper();
}

string foo =          


        
相关标签:
1条回答
  • 2021-01-20 10:33

    While you can only refer to the outer foo after you declared it, locals are allocated at the beginning of a function which means the inner foo will overshadow the outer one, even if it hasn't been declared yet.

    0 讨论(0)
提交回复
热议问题