Using Yield in Practice
The moral of the story is STOP thinking so
hard about it, just use "yield return" the next piece of data in
the list. When using the yield statment GetEnumerator's job is to answer
foreach's question - "what's my next item please". Walk all the items in
your collection and yield return what you want. Don't worry about
remembering where you were - that's all part of the generated statemachine goo
- it will stop and start the function you write as it pleases in it's MoveNext
implementation or whatever (cause we dont really care - we're not
suppposed to be thinking, remember?) . =)
[Via jfo's
coding]
I'll admit, I haven't thought a whole lot about yield because, well, I
couldn't find anything that described what it did in terms I could understand. I
just kept thinking "oh, cool, another new feature in .net 2.0 that I'll get to
learn by the time 3.0 comes out" and moved on to the next task.
Thinking about it as kind of a "macro" (similar to what a using(){} block
does) lets it gel a little better.