Using yield return
turns a method into an iterator. The return type of an iterator must be IEnumerable
, IEnumerable<T>
, IEnumerator
, or IEnumerator<T>
.
I have found that an iterator can only be used in a foreach
loop if it returns one of the IEnumerable
types. Given this limitation, when would it make sense to choose an IEnumerator
return type instead of an IEnumerable
?