C
C#3w ago
Faker

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:
C#
foreach (int i in ProduceEvenNumbers(9))
{
Console.Write(i);
Console.Write(" ");
}
// Output: 0 2 4 6 8

IEnumerable<int> ProduceEvenNumbers(int upto)
{
for (int i = 0; i <= upto; i += 2)
{
yield return i;
}
}
C#
foreach (int i in ProduceEvenNumbers(9))
{
Console.Write(i);
Console.Write(" ");
}
// Output: 0 2 4 6 8

IEnumerable<int> ProduceEvenNumbers(int upto)
{
for (int i = 0; i <= upto; i += 2)
{
yield return i;
}
}
the IEnumerable is an interface, right? We can use interface as data types? why use interface as data types?
12 Replies
Pobiega
Pobiega3w ago
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"
Faker
FakerOP3w ago
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
Pobiega
Pobiega3w ago
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 tim
yes. the rest is just word soup to me
Faker
FakerOP3w ago
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 ?
Pobiega
Pobiega3w ago
? List<T> implements IEnumerable<T>
Faker
FakerOP3w ago
AH sorry I miss read yeah I see the enumerable is the thing that return the IEnumerable<> data type, right ?
Pobiega
Pobiega3w ago
no the enumerable is the IEnumerable<T> itself
Faker
FakerOP3w ago
C#

foreach (int i in ProduceEvenNumbers(9))
{
Console.Write(i);
Console.Write(" ");
}
// Output: 0 2 4 6 8

IEnumerable<int> ProduceEvenNumbers(int upto)
{
for (int i = 0; i <= upto; i += 2)
{
yield return i;
}
}
C#

foreach (int i in ProduceEvenNumbers(9))
{
Console.Write(i);
Console.Write(" ");
}
// Output: 0 2 4 6 8

IEnumerable<int> ProduceEvenNumbers(int upto)
{
for (int i = 0; i <= upto; i += 2)
{
yield return i;
}
}
Here for example, ProduceEvenNumbers return an IEnumerable<int>, so what is the enumerable here?
Pobiega
Pobiega3w ago
the thing that gets returned when you call that method
Faker
FakerOP3w ago
ahhh yep I see, what's the difference between an iterator pls. I know enumerable has enumerator and enumerator can generate iterators (I think)
Pobiega
Pobiega3w ago
essentially, foreach when you use foreach on an IEnumerable, this happens:
foreach(var item in collection)
{
Console.WriteLine(item.ToString());
}
// becomes...

IEnumerator<int> enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
var item = enumerator.Current;
Console.WriteLine(item.ToString());
}
foreach(var item in collection)
{
Console.WriteLine(item.ToString());
}
// becomes...

IEnumerator<int> enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
var item = enumerator.Current;
Console.WriteLine(item.ToString());
}
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 about
Faker
FakerOP3w ago
yep I see, thanks !

Did you find this page helpful?