yield语句执行 [英] yield statement implementation

查看:195
本文介绍了yield语句执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的所有的有关收益率语句,在一个易于理解的形式。

I want to know everything about the yield statement, in an easy to understand form.

我已阅读有关收益率语句和实施迭代器模式时,它的易用性。然而,大多数的这是非常干燥的。我想获得在幕后,看看微软是如何处理的回报率。

I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and see how Microsoft handles return yield.

此外,当你用产量突破?

Also, when do you use yield break?

推荐答案

收益率的工作原理是在内部构建一个状态机。它存储了当前的工作状态,当它退出和恢复从该状态下一次。

yield works by building a state machine internally. It stores the current state of the routine when it exits and resumes from that state next time.

您可以使用反射来看看它是如何由编译器实现。

You can use Reflector to see how it's implemented by the compiler.

产量突破时,要停止返回结果的使用。如果你没有一个产量突破,编译器会假定一个在函数(就像一个回复结束; 语句在一个功能正常)

yield break is used when you want to stop returning results. If you don't have a yield break, the compiler would assume one at the end of the function (just like a return; statement in a normal function)

这篇关于yield语句执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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