yield语句执行语句、yield

2023-09-02 01:32:57 作者:你是我年少时最冒险的梦

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

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)