An IEnumerator is a thing that can enumerate: it has the Current property and the MoveNext and Reset methods (which in . NET code you probably won't call explicitly, though you could). An IEnumerable is a thing that can be enumerated...which simply means that it has a GetEnumerator method that returns an IEnumerator .
IEnumerable. IEnumerable<T> contains a single method that you must implement when implementing this interface; GetEnumerator, which returns an IEnumerator<T> object. The returned IEnumerator<T> provides the ability to iterate through the collection by exposing a Current property.
IEnumerable- It is an Interface and Exposes the enumerator, which supports a simple iteration over a collection of a specified type. Enumerable- It is a class and Provides a set of static methods for querying objects that implement IEnumerable.
The C# GetEnumerator() method is used to convert string object into char enumerator. It returns instance of CharEnumerator. So, you can iterate string through loop.
When I emplement IEnumerable<T>
interface I see two GetEnumerator
methods: one returning IEnumerator
and other IEnumerator<T>
. When would I use one or another?
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