Can anyone please explain to me what is the difference between IEnumerable & IEnumerator , and how to use them?
Thanks!!!
Generally, an IEnumerable is an object which can be enumerated, such as a list or array. An IEnumerator is an object that stores the state of the enumeration.
The reason they're not one and the same is that you could have multiple enumerations over the same object at the same time - even in a single-threaded application. For example, consider the following code:
foreach (x in mylist)
{
foreach (y in mylist)
{
if (x.Value == y.Value && x != y)
{
// Found a duplicate value
}
}
}
This would work fine if mylist is a proper implementation of IEnumerable, but it would fail if mylist returned itself as the enumerator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With