Maciek Maciek - 9 months ago 96
C# Question

What is yield return in C#?

Possible Duplicate:

What is the yield keyword used for in C#?

I've recently noticed the "yield" keyword and it caught my attention. English is not my primary language so the meaning of the word itself may elude me as well. What does that keyword mean in C# and how is it used?


yield is a contextual keyword used in iterator methods in C#. Basically, it has two use cases:

  • yield return obj; returns the next item in the sequence.
  • yield break; stops returning sequence elements (this happens automatically if control reaches the end of the iterator method body).