相关疑难解决方法(0)

for循环中计数器变量的范围是多少?

我在Visual Studio 2008中收到以下错误:

Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else

这是我的代码:

for (int i = 0; i < 3; i++)
{
  string str = "";
}

int i = 0; // scope error
string str = ""; // no scope error
Run Code Online (Sandbox Code Playgroud)

我知道str一旦循环终止就不再存在,但我也认为范围i也局限于for循环.

那么i与在for循环之外声明的变量具有相同的范围吗? …

c c# scope

9
推荐指数
1
解决办法
4363
查看次数

标签 统计

c ×1

c# ×1

scope ×1