Is 'yield' keyword a syntactic sugar ? What is its Implementation

Possible Duplicate:
yield statement implementation

I've seen msdn docs and it says:

The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object.

So it means yield keyword is a Syntactic sugar and compiler does the heavy work of generating the Iterator. (Am I Correct ?)

Then what is the generated implementation code for this syntactic sugar.


The generated code depends on the original, but generally speaking a state machine gets generated which keeps track of the current state of the collection.

See yield statement implementation, this answer by Eric Lippert and this blog post by Jon Skeet.

链接地址: http://www.djcxy.com/p/53774.html

上一篇: 在Nerd晚餐教程中有趣地使用了C#yield关键字

下一篇: 'yield'关键字是一个语法糖吗? 它的实现是什么