可能重複:
Child Scope & CS0136
C# Variable Scoping如何在C#局部範圍以外的工作
雖然我一直在使用C#相當長的一段時間裏,我只是在這個錯誤絆倒。
如果我有以下幾點:
if(true)
{
int x = 0;
}
int x = 0;
我得到一個錯誤,指出:A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a child scope to denote something else.
如果我這樣做:
if(true)
{
int x = 0;
}
x = 0;
我得到一個錯誤,指出:The name 'x' does not exist in the current context.
我可以理解有一個或另一個,但爲什麼這兩個這些錯誤是否存在?有沒有解決第一個選擇的方法?我覺得很煩人。
謝謝。
請參閱http://stackoverflow.com/a/296780/870604 – ken2k
@評論:是的,對不起。猜猜我應該先做一些搜索......: -/ – Eric
@Eric,以防萬一你真的想讓自己和其他人感到痛苦和痛苦,下面是一些類似x的人物的例子C#中的有效標識符:'intҲ,ҳ,א,ẋ,ẍ;'我相信如果您選擇將這些字符混合到您的源代碼中,以後沒有人會感謝您。 :)(以防止任何人閱讀此內容...請不要使用這些異國情調的字符,以便您可以使用類似外觀的名稱具有不同的變量) –