考虑以下代码:
List<string> things = new List<string>();
foreach (string thing in things)
{
string foo = thing.ToUpper();
}
string foo = String.Empty;
为什么编译器抱怨 foo 被声明两次?难道在 foreach 循环中声明的实例只在循环范围内生效吗?
考虑以下代码:
List<string> things = new List<string>();
foreach (string thing in things)
{
string foo = thing.ToUpper();
}
string foo = String.Empty;