When implementing iterator using yield return
, is there any difference between returning IEnumerator
and IEnumerable
?

- 47,437
- 25
- 129
- 188

- 20,472
- 39
- 112
- 155
-
possible duplicate of [Can anyone explain IEnumerable and IEnumerator to me?](http://stackoverflow.com/questions/558304/can-anyone-explain-ienumerable-and-ienumerator-to-me) – CodesInChaos Feb 22 '12 at 14:41
-
http://stackoverflow.com/questions/2635818/ienumerable-ienumerator – CodesInChaos Feb 22 '12 at 14:41
-
http://stackoverflow.com/questions/4548464/question-regarding-ienumerable-and-ienumerator – CodesInChaos Feb 22 '12 at 14:41
-
2C'mon, this is **not a duplicate** of the referenced questions! This question is about an entirely different subject: the OP understands the difference between IEnumerator and IEnumarble, and is asking about using them in a specific context of yield return. – Sergey Kalinichenko Feb 22 '12 at 14:43
3 Answers
IEnumerable
and IEnumerator
are two different things.
IEnumerable<T>
is a sequence that can be iterated over.
IEnumerator<T>
is an object that is returned by IEnumerable<T>
to iterate once over the sequence.
In general, the only place to return IEnumerator<T>
is in the GetEnumerator()
method.
yield return
behaves the same way for both types, except that an iterator method that returns IEnumerable<T>
can execute multiple times (each time the sequence is enumerated).
For more information on how this works, see Jon Skeet's article.

- 868,454
- 176
- 1,908
- 1,964
-
1With all due respect, this is not what the OP is asking about. He's asking specifically about `yield return` methods returning one or the other; I didn't even know it was OK to `yield return` from a function returning an IEnumerator
. – Sergey Kalinichenko Feb 22 '12 at 14:46 -
@dasblinkenlight: `yield return` behaves the same way for both. AFAICT, he's just asking what the difference between the two types is. – SLaks Feb 22 '12 at 14:47
-
I disagree, because he specifically says "when implementing itertaor **using yeild return**..." I think that your comment about there being no difference is a much better answer to the OP's question. – Sergey Kalinichenko Feb 22 '12 at 14:49
As mentioned by SLaks, an Enumerator can be iterated over once, and an IEnumerable can generate any number of Enumerators, allowing the underlying collection to be iterated over multiple times.
In practice, the primary difference is that there are lots and lots of methods, such as LINQ, and many methods for interacting with collections that all deal with Enumerables, not Enumerators, so an Enumerator simply won't be able to be as widely used.

- 202,030
- 26
- 332
- 449
-
A couple of places you've said "Enumerable" where I think you meant to say "Enumerator": "...any number of *Enumerators*...", "...so an *Enumerator* simply won't..." – Dr. Wily's Apprentice Feb 22 '12 at 14:45
As the implementer of the iterator function, it doesn't have an effect on you. It matters more to the consumer of the iterator function.
In general, people prefer to have an IEnumerable<T>
object, because you can do a foreach loop on it, and if you really need an IEnumerator<T>
object, then you can get one from the IEnumerable<T>
object's GetEnumerator()
method.

- 10,212
- 1
- 25
- 27