在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#对局部变量的声明更严格。而之所以有这样的规定,我想主要目的是防止编程的无意之中的错误。
贴子以"现状"提供且没有任何担保也没有授予任何权利
打印 | 张贴于 2004-10-26 11:49:00 | Tag:C#
留言反馈
{
str=str.Trim();//错误!
}
str在这种情况下是只读的。
{
b=System.Trim(b);
if(b!="")//如果客户端没有提交此值,则不会产生相应的SQL语句。
{
if(a=="File_Vol_ID")
{
sql = sql + " and (" + a + " = '" + b.Replace("'","").Trim() + "')";
}
else
{
sql = sql + " and (" + a + " like '%" + b.Replace("'","").Trim() + "%')";
}
}
return sql;
}
我很奇怪,为什么C#的语言规范要放到VC文件夹中。这算不算一个bug呢?真不知道如果在安装VS.NET的时候没有选装Visual C++ 7 这份C#语言规范要放在哪里。
我最讨厌的是这个也禁止了:
foreach(string str in strs)
{
str=str.Trim();//错误!
}
try
{
int a = 1;
}
catch
{
char a = 'a';
}
我想对windows Xp里的某个文件夹加个密码,
不知用C#如何来简单实现,你能否指点一下?thanks !