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

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

Given this code:

List things = new List();

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

string foo =          


        
1条回答
  •  猫巷女王i
    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)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题