for循环中声明的变量是局部变量? [英] Variable declared in for-loop is local variable?

查看:272
本文介绍了for循环中声明的变量是局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用C#在相当长的时间,但从来没有意识到以下内容:

I have been using C# for quite a long time but never realised the following:

 public static void Main()
 {
     for (int i = 0; i < 5; i++)
     {

     }

     int i = 4;  //cannot declare as 'i' is declared in child scope                
     int A = i;  //cannot assign as 'i' does not exist in this context
 }



那么,为什么我不能用'我'的块外,如果它不允许我宣布这个名称的变量的值?

So why can I not use the value of 'i' outside of the for block if it does not allow me to declare a variable with this name?

我认为迭代变量使用一个for循环只有在其范围内是有效的。

I thought that the iterator variable used by a for-loop is valid only in its scope.

推荐答案

您不能定义与变量的原因相同的名称中都为环以及外部for循环是因为在外部范围变量是在内侧-范围有效。 。这意味着将有for循环中的两个'我'的变量,如果这被允许

The reason you are not allowed to define a variable with the same name in both the for-loop as well as outside the for-loop is because variables in the outer-scope are valid in the inner-scope. Meaning that there would be two 'i' variables within the for-loop if this was allowed.

请参阅:的 MSDN斯科普斯

具体做法是:

在一个局部变量声明
(第8.5.1节)声明的局部变量的范围是在该块该声明所在。

The scope of a local variable declared in a local-variable-declaration (Section 8.5.1) is the block in which the declaration occurs.

在声明的局部变量的范围为,初始化为
语句(第8.8.3)的是换初始化,for条件,
中的迭代器,以及所包含的声明。for语句

The scope of a local variable declared in a for-initializer of a for statement (Section 8.8.3) is the for-initializer, the for-condition, the for-iterator, and the contained statement of the for statement.

和也:的本地变量声明(C#的规范第8.5.1节)

And also: Local variable declarations (Section 8.5.1 of the C# specification)

具体做法是:

在一个局部变量声明
声明的局部变量的范围是块其中发生的声明。
这是指
局部变量在之前的
局部变量的局部变量声明的文本位置错误。 在一个
局部变量的范围内,这是一个编译时错误申报另一个本地
变量或常量具有相同的名称。

(重点煤矿。)

这意味着的范围I 里面的for循环是for循环。而 I 之外的范围的for循环是整个main方法的的for循环。这意味着你不得不的两次出现我循环按照上述这是无效的内部。

Which means that the scope of the i inside your for-loop, is the for-loop. Whereas the scope of the i outside of your for-loop is the entire main method plus the for-loop. Meaning you'd have two occurrences of i inside the loop which is invalid according to the above.

原因你为什么不能做 INT A = I; 是因为 INT I 只范围使用内循环。因此,它不再是循环以外访问。

The reason why you're not allowed to do int A = i; is because int i is only scoped for use within the for loop. Thus it is no longer accessible outside of the for loop.

正如你可以看到这两个问题是范围界定的结果;第一个问题( INT I = 4; )将导致两个 I 中变量循环范围。而 INT A = I; 将导致访问变量超出范围

As you can see both of these issues are a result of scoping; the first issue (int i = 4;) would result in two i variables within the for loop scope. Whereas int A = i; would result in access to a variable that is out of scope.

尽你所能这样做反而是声明 I 先限定整个方法,然后用它在这两个方法以及for循环的范围。这将避免破坏或者规则

What you could do instead is declare i to be scoped to the entire method, and then use it in both the method as well as the for-loop scope. This will avoid breaking either rule.

public static void Main()
{
    int i;

    for (i = 0; i < 5; i++)
    {

    }

    // 'i' is only declared in the method scope now, 
    // no longer in the child scope -> valid.
    i = 4;

    // 'i' is declared in the method's scope -> valid. 
    int A = i;
}

修改

C#编译器当然也可以更改为允许此代码相当有效编译。毕竟这是有效的:

The C# compiler could of course be changed to allow this code to compile quite validly. After all this is valid:

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

for (int i = 5; i > 0; i--)
{
    Console.WriteLine(i);
}



但是,如果真的是你的代码的可读性和可维护性能有利写代码,如:

But would it really be beneficial to your code readability and maintainability to be able to write code such as:

public static void Main()
{
    int i = 4;

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(i);
    }

    for (int i = 5; i > 0; i--)
    {
        Console.WriteLine(i);
    }

    Console.WriteLine(i);
}



想想这里犯错的可能性,请问最后一个打印出0或4?现在,这是一个非常小的例子,其中一个是很容易跟踪和追踪,但它绝对是一个很多比已宣布外更少的维护和可读性我按不同的名称。

Think about the potential for mistakes here, does the last i print out 0 or 4? Now this is a very small example, one which is quite easy to follow and track but it is definitely a lot less maintainable and readable than having declared the outer i by a different name.

注:

请注意,C#的作用域规则从的 C ++的作用域规则的。在C ++变量是仅在从它们被声明,其中,直到该块的末端的范围。这将使你的代码在C ++中的有效构造。

Please note, C#'s scoping rules differ from C++'s scoping rules. In C++ variables are only in scope from where they are declared until the end of the block. Which would make your code a valid construct in C++.

这篇关于for循环中声明的变量是局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆