Friday, April 14, 2006 3:13 PM Christopher

the yield statement - not so mysterious after all?

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.

Filed under:

Comments

# re: the yield statement - not so mysterious after all?

Wednesday, May 05, 2010 12:24 PM by Ella Baker

Truly cool post as for me. It would be nice to read more concerning this topic. Thanx for sharing such info.

Ella Baker

<a href="www.waybiz.com/">b2b internet marketing</a>

# re: the yield statement - not so mysterious after all?

Saturday, April 30, 2011 2:13 PM by Kate Benedict

Pretty interesting place you've got here. Thanx for it. I like such themes and anything that is connected to them. I definitely want to read more on that blog soon.  

Kate Benedict    

<a href="www.pickescort.com/">south London escort</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)