yield statement implementation

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)

Leave a Comment