已经的foreach的使用变量在C#5被改变? [英] Has foreach's use of variables been changed in C# 5?

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

问题描述

在此答案 http://stackoverflow.com/a/8649429/1497 埃里克利珀说:我们FYI极有可能在C#中的下一个版本来解决这个问题;这是一个重大的痛苦点,开发商与问候的foreach循环如何使用变量


  

在下一版本每次通过foreach循环运行时,我们将产生一个新的循环变量,而不是在同一个变量每次关闭。这是一个破的变化,但在绝大多数情况下的破将被修复,而不是造成错误。


我一直没能找到任何说明这种变化尚未作出。是否有任何迹象表明这是foreach循环将如何工作在C#5?


解决方案

这是一个更改为C#语言,而不是.NET框架。因此,它只影响code在C#5.0编写,无论.NET framework版本上的code将被执行。

C#5.0

该规范第8.8.4明确指出,这种变化已经取得进展。具体来说,C#5.0规范规定的249页:

 的foreach(以x垂直V)嵌入语句


  

时,再扩展到:


  {
    E E =((C)(X))的GetEnumerator()。
    尝试{
        而(e.MoveNext()){
            V V =(V)(T)的e.Current;
            嵌入语句
        }
    }
    最后{
        ... //处置Ë
    }
}

和更高版本:


  

while循环中v的位置是多么重要
  通过发生在任何匿名函数捕获
  嵌入语句。


C#4.0

与其中列明了C#4.0规范进行比较时,这种变化对规范是明确的(同样,在第8.8.4,但是这一次,247页):

 的foreach(以x垂直V)嵌入语句


  

时,再扩展到:


  {
    E E =((C)(X))的GetEnumerator()。
    尝试{
        V伏;
        而(e.MoveNext()){
            V =(V)(T)的e.Current;
            嵌入语句
        }
    }
    最后{
        ... //处置Ë
    }
}

注意变量 v 声明的循环,而不是境内关外,因为它是用C#5.0。

注意

您可以找到在在Visual Studio的安装文件夹的C#规范VC#\\规格\\ 1033 。这是VS2005,VS2008,VS2010和VS2012的情况下,让您获得规格为C#1.2,2.0,3.0,4.0和5.0。您也可以通过搜索 C#规格找到MSDN上的规格。

In this answer http://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#\Specifications\1033. 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.

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

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