How is Yield Return Implemented Under the Hood

T

Travis Parks

Hello:

How are iterators implemented under the hood? Is this performed using
threading? or is there a more complex mechanism under the hood?

Thanks,
Travis Parks
 
T

Travis Parks

Wouldn't threading be complicated enough?  :)

You should download Red-Gate's Reflector utility.  It will allow you to
look at the compiled output from your own program, to see how the
compiler has transformed things like iterator methods.  (It does lots
more than that, but that's the useful thing it does in this context :)
).  You could also use the ILDASM.EXE tool, but Reflector will decompile
back to C# or other languages if you want it to, which makes it easier
to follow the code.

The short answer:

The C# compiler transforms your iterator method into a class that
implements IEnumerator<T> or IEnumerator (as appropriate), which in turn
contains a MoveNext() method that is essentially a state machine.  The
compiler takes your original code, inserts labels, and then includes a
switch to handle jumping to the correct location given the current state
of your state machine.

If you're familiar with coroutines, it's sort of like that.

So, any place you write a "yield" statement, the new method will save a
state variable to remember where it was in the method, copy the value
you specified (if it's a "yield return") to the Current property, and
then return.  The next time the MoveNext() method is called, it uses the
state variable to figure out where to resume execution within the method.

So…iterators don't use threading (unless you've explicitly introduced
threading into your iterator method, of course).  But they definitely
use a somewhat complex mechanism.  :)

Pete

I saw someone giving a similar example when I looked it up on google,
but it seemed very specific to one problem. I was wondering if the
same mechanism was used in all cases.

I guess I should ask: between calls, how does it maintain the state of
local variables? Does it make all local variables fields? I'll try
downloading the program to find out.

I wonder if this is the same way Python's generator functions are
implemented...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top