这是怎么回事了“的foreach”循环的幕后? [英] What's going on behind the scene of the 'foreach' loop?

查看:135
本文介绍了这是怎么回事了“的foreach”循环的幕后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
如何foreach循环在C#中工作?

我一直在搜索互联网,我无法找到任何答案来到底发生了什么与C#中的的foreach 循环幕后。

I've been searching the internet and I'm having trouble finding any answers as to what's really going on behind the scenes with the foreach loop in C#.

我知道这个问题并没有真正涉及到实际的编码,但它困扰着我。我是相当新的面向对象编程,尤其是接口。我明白他们是合同,我知道如何的IEnumerable 的IEnumerator 工作 - 还是让我觉得

I know this question doesn't really pertain to actually coding but its bothering me. I'm pretty new to OO programming and especially interfaces. I understand they are contracts and I understand how IEnumerable and IEnumerator work - or so I think.

我一直在阅读这篇文章在MSDN上:
IEnumerable接口

I've been reading this article on MSDN: IEnumerable Interface

我明白了一切是如何设置的。我有点不清楚,虽然在循环如何使用的foreach 知道通过所有的值<$到itterate C $ C> _People 。它是如何知道的?它是如何跟踪电流的只需调用返回新PeopleEnum(_People);

I understand how everything is set up. I'm a little unclear though in the Main loop how the foreach knows to itterate through all the values in _people. How does it know this? How does it keep track of Current by just calling return new PeopleEnum(_people);?

修改:我看不出这是一个精确的副本。是被要求的类似理由和相同的问题,但我们正在寻找不同的答案的我想要的答案并没有在上一个问题的讨论。

EDIT: I don't see how this is an exact duplicate. Yes its similar grounds and the same question is being asked but we are looking for different answers or the answer I wanted was not discussed in the previous question.

喜欢的foreach foreach循环(INT我的obj){...}还挺相当于

A foreach loop like foreach(int i in obj) {...} kinda equates to

...是有点不回答我要找的。

... is "kinda" not the answer I'm looking for.

推荐答案

我建议你阅读C#规范,回答您的详细问题的部分8.8.4。从它引用在这里为您提供方便:

I encourage you to read section 8.8.4 of the C# specification, which answers your question in detail. Quoting from it here for your convenience:

格式

foreach (V v in x) embedded-statement

时,再扩展到:

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






该类型E,C,V和T是枚举,通过语义分析推导的收集,循环变量和收集元素类型;详情请参见规范。


The types E, C, V and T are the enumerator, collection, loop variable and collection element types deduced by the semantic analyzer; see the spec for details.

所以你去。 A的foreach只是一个写一个while循环调用的MoveNext直到MoveNext将返回false

So there you go. A "foreach" is just a more convenient way of writing a "while" loop that calls MoveNext until MoveNext returns false.

一些微妙的东西更方便的方法:

A few subtle things:


  • 这不需要是生成的代码;所有需要的是,我们产生了产生相同的结果的代码。例如,如果你的foreach一个数组或字符串,我们只是生成一个for循环(或循环,在多-D阵列的情况下)的索引数组或字符串的字符,而不是把上分配一个枚举的费用。

  • This need not be the code that is generated; all that is required is that we generate code that produces the same result. For example, if you "foreach" over an array or a string, we just generate a "for" loop (or loops, in the case of multi-d arrays) that indexes the array or the chars of the string, rather than taking on the expense of allocating an enumerator.

如果枚举是值类型,那么处置代码可能会或可能不会选择处置前框枚举。不要依赖于一个或其他方式。 (见的 http://blogs.msdn.com/b/ericlippert/archive/2011/03/14/to-box-or-not-to-box-that-is-the-question.aspx 了相关的问题。)

If the enumerator is of value type then the disposal code might or might not choose to box the enumerator before disposing it. Don't rely on that one way or the other. (See http://blogs.msdn.com/b/ericlippert/archive/2011/03/14/to-box-or-not-to-box-that-is-the-question.aspx for a related issue.)

同样,如果石膏自动插入以上被确定为身份转换,则强制类型转换可能会被省略,即使这样做通常会导致一个值类型被复制。

Similarly, if the casts automatically inserted above are determined to be identity conversions then the casts might be elided even if doing so would normally cause a value type to be copied.

C#的未来版本很可能把循环变量的声明 v 的while循环体;这将防止被报道了上一次堆栈溢出,每天常见的修改封错误。 [更新: ]这种变化确实在C#5 得到落实。

Future versions of C# are likely to put the declaration of loop variable v inside the while loop body; this will prevent the common "modified closure" bug that is reported about once a day on Stack Overflow. [Update: This change has indeed been implemented in C# 5.]

这篇关于这是怎么回事了“的foreach”循环的幕后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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