在 C# 5 中,foreach 对变量的使用是否有所改变? [英] Has foreach's use of variables been changed in C# 5?

查看:20
本文介绍了在 C# 5 中,foreach 对变量的使用是否有所改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个答案中https://stackoverflow.com/a/8649429/1497 Eric Lippert 说仅供参考,我们极有可能在下一版本的 C# 中解决这个问题;这是开发人员的一个主要痛点"关于 foreach 循环如何使用变量.

<块引用>

在下一个版本中,每次运行foreach"循环时,我们都会生成一个新的循环变量,而不是每次都关闭同一个变量.这是一个重大"更改,但在绝大多数情况下,中断"将是修复而不是导致错误.

我还没有找到任何表明此更改已完成的内容.是否有任何迹象表明这就是 foreach 循环在 C# 5 中的工作方式?

解决方案

这是对 C# 语言的更改,而不是对 .NET 框架的更改.因此,它只影响在 C# 5.0 下编译的代码,而不管该代码将在哪个 .NET 框架版本上执行.

C# 5.0

规范的第 8.8.4 节明确表示已进行此更改.具体来说,C# 5.0 规范的第 249 页指出:

foreach (V v in x) 嵌入语句

<块引用>

然后扩展为:

<代码>{E e = ((C)(x)).GetEnumerator();尝试 {而 (e.MoveNext()) {V v = (V)(T)e.Current;嵌入语句}}最后 {...//处理 e}}

后来:

<块引用>

在 while 循环中 v 的位置对于它的方式很重要被任何匿名函数捕获嵌入语句.

C# 4.0

与 C# 4.0 规范相比,规范的这一变化很明显(同样,在第 8.8.4 节,但这次是第 247 页):

foreach (V v in x) 嵌入语句

<块引用>

然后扩展为:

<代码>{E e = ((C)(x)).GetEnumerator();尝试 {Ⅴ;而 (e.MoveNext()) {v = (V)(T)e.Current;嵌入语句}}最后 {...//处理 e}}

请注意变量 v 是在循环外声明的,而不是在循环内声明,就像在 C# 5.0 中一样.

注意

您可以在VC#Specifications1033下的Visual Studio安装文件夹中找到C#规范.这是 VS2005、VS2008、VS2010 和 VS2012 的情况,让您可以访问 C# 1.2、2.0、3.0、4.0 和 5.0 的规范.您还可以通过搜索 C# Specification 在 MSDN 上找到规范.

In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that "FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers" with regards to how the foreach loops uses the variable.

In the next version each time you run through the "foreach" loop we will generate a new loop variable rather than closing over the same variable every time. This is a "breaking" change but in the vast majority of cases the "break" will be fixing rather than causing bugs.

I have not been able to find anything indicating that this change has been made yet. Is there any indication that this is how the foreach loop will work in C# 5?

解决方案

This is a change to the C# language, not the .NET framework. Therefore, it only affects code compiled under C# 5.0, regardless of the .NET framework version on which that code will execute.

C# 5.0

Section 8.8.4 of the specification makes it clear that this change has been made. Specifically, page 249 of the C# 5.0 specification states:

foreach (V v in x) embedded-statement

is then expanded to:

{
    E e = ((C)(x)).GetEnumerator();
    try {
        while (e.MoveNext()) {
            V v = (V)(T)e.Current;
            embedded-statement
        }
    }
    finally {
        … // Dispose e
    }
}

And later:

The placement of v inside the while loop is important for how it is captured by any anonymous function occurring in the embedded-statement.

C# 4.0

This change to the specification is clear when comparing with the C# 4.0 specification which states (again, in section 8.8.4, but this time, page 247):

foreach (V v in x) embedded-statement

is then expanded to:

{
    E e = ((C)(x)).GetEnumerator();
    try {
        V v;
        while (e.MoveNext()) {
            v = (V)(T)e.Current;
            embedded-statement
        }
    }
    finally {
        … // Dispose e
    }
}

Note that the variable v is declared outside the loop instead of inside, as it is with C# 5.0.

Note

You can find the C# specification in the installation folder of Visual Studio under VC#Specifications1033. This is the case for VS2005, VS2008, VS2010 and VS2012, giving you access to specifications for C# 1.2, 2.0, 3.0, 4.0 and 5.0. You can also find the specifications on MSDN by searching for C# Specification.

这篇关于在 C# 5 中,foreach 对变量的使用是否有所改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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