在C/C++下合法的语句:
int test = 1;
if (test != 0)
{
char test = 'a';
}
却不能在C#下编译,得到CS0136的错误: A local variable named 'test' cannot be declared in this scope because it would give a different meaning to 'test', which is already used in a 'parent or current' scope to denote something else,
在C#规范8.5.1之中对此作了明确的规定:Within the scope of a local variable, it is a compile-time error to declare another local variable or constant with the same name.
所以C#对局部变量的声明更严格。而之所以有这样的规定,我想主要目的是防止编程的无意之中的错误。
贴子以"现状"提供且没有任何担保也没有授予任何权利