可能重复:
子范围和 CS0136
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.
我可以理解有一个或另一个,但为什么这两个错误都存在?有没有办法绕过第一个选项?我觉得很烦人。
谢谢。