What is an Enumerator, Iterator, IEnumerable and IEnumerator and relation with yield return keyword
Hello guys, from what I've read, an enumerator is what get returns from an IEnumerable, which is itself a sequence of element that can be iterated over.
So basically, an enumerable gives us a series of element. Then on these series of element, one by one, we can get an "enumerator" which will allow us to access each one of them one at a time. The enumerator uses methods to manually control the looping process. However, an enumerator can also return an iterator which does the exact same job behind the scenes, it's just syntax sugar; using the command
yield return
, we return an iterator which behind the scenes will just methods like .MoveNext() from an enumerator.
It's still a bit unclear in my head, I believed I mixed things up, would really appreciate if someone can add to what I've said and correct me where I went wrong please.
Also, one thing, consider the following code:
the IEnumerable is an interface, right? We can use interface as data types? why use interface as data types?12 Replies
why use interface as data types?Sometimes its just very nice to return an IEnumerable<T> instead of a List<T> or T[], because it tells the caller "you will get a readonly sequence of elements back. you should not try to add to this sequence, or further manipulate it"
ah I see
IEnumerable are readonly sequences? (or everything that implements/extends it?)
by the way, was their any things wrong stated in y other answers pls
IEnumerable<T>
are potential sequences, essentially. But stuff that implements it can be anything it wants. Arrays, lists, dictionaries, hashtables etc all implement it.
the IEnumerable is an interface, right? We can use interface as data types?yes. yes.
Then on these series of element, one by one, we can get an "enumerator"no, the elements dont have enumerators - the enumerable has one. as you "iterate over it", it yields the items of the series/sequence
which will allow us to access each one of them one at a timyes. the rest is just word soup to me
what do you mean by "stuff that implements it can be anything" pls, like IEnumerable return a sequence; not that sequence, we can implement it in whatever data structure we want that can be iterated through ?
?
List<T>
implements IEnumerable<T>
AH
sorry I miss read
yeah I see
the enumerable is the thing that return the IEnumerable<> data type, right ?
no
the enumerable is the
IEnumerable<T>
itself
Here for example, ProduceEvenNumbers return an IEnumerable<int>, so what is the enumerable here?
the thing that gets returned when you call that method
ahhh
yep I see, what's the difference between an iterator pls. I know enumerable has enumerator and enumerator can generate iterators (I think)
essentially,
foreach
when you use foreach
on an IEnumerable, this happens:
https://learn.microsoft.com/en-us/dotnet/csharp/iterators for more info. it calls methods that use yield return
"iterator methods"
so that might be what you are on aboutyep I see, thanks !