I have seen the yield keyword being used quite a lot on Stack Overflow and blogs. I don’t use LINQ. Can someone explain the yield keyword?
I know that similar questions exist.
But none really explain what is its use in plain simple language.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By far the best explanation of this (that I’ve seen) is Jon Skeet’s book – and that chapter is free! Chapter 6, C# in Depth. There is nothing I can add here that isn’t covered.
Then buy the book; you will be a better C# programmer for it.
Q: Why didn’t I write a longer answer here (paraphrased from comments); simple. As Eric Lippert observes (here), the
yieldconstruct (and the magic that goes behind it) is the single most complex bit of code in the C# compiler, and to try and describe it in a brief reply here is naïve at best. There are so many nuances toyieldthat IMO it is better to refer to a pre-existing (and fully qualified) resource.Eric’s blog now has 7 entries (and that is just the recent ones) discussing
yield. I have a vast amount of respect for Eric, but his blog is probably more appropriate as a “further information” for people who are comfortable with the subject (yieldin this case), as it typically describes a lot of the background design considerations. Best done in the context of a reasonable foundation.(and yes, chapter 6 does download; I verified…)