参考代码
public void MyMethod()
{
string x;
List sampleList = populateList();
foreach(MyType myType in sampleList)
{
string x; // why is this not allowed?
doSomethingwithX(x);
}
}
我最近开始学习 C#,今天遇到了与上面类似的代码的问题。VS2010 用这条消息标记了注释代码a local variable named x cannot be declared in this scope because it would give a different meaning to variable x which is already used in parent or current scope to denote something else
....
我不明白...不是块语句和范围界定的全部本质...我知道我可以更改我的变量名并继续。但我想知道为什么?