Is the word yield syntactic sugar? What is its implementation

Possible duplicate:
release report implementation

I saw msdn docs and it says:

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

So this means that the yield keyword is syntactic sugar, and the compiler does the hard work of creating an Iterator. (Right?)

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

+6
source share
1 answer

The generated code depends on the original, but as a rule, a state machine is generated that tracks the current state of the collection.

See the implementation of the release report , this Eric Lippert answer and this blog post from Jon Skeet.

+4
source

Source: https://habr.com/ru/post/902487/


All Articles