When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator<T> for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type.

Yield keyword has two forms:

  • yield return <expression>; (expression is defined in return type)
  • yield break;                                  (yield break is used to end iteration)


Yield must be of return type IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.


Yield keyword is consumed by an iterator method by using a foreach statement or LINQ query.