IEnumerable
is an interface in C# that defines one method, GetEnumerator()
, which returns an IEnumerator
interface. This interface is commonly used when dealing with collections and reading a collections items. It allows readonly access to a collection, and a collection that implements IEnumerable
can be used with a foreach
statement.
IEnumerator
is another interface that helps to get current elements from the collection. It has two methods: MoveNext()
, which sets the enumerator to the next element of the collection and returns true if the enumerator was successfully set to the next element and false if the enumerator has reached the end of the collection, and Reset()
, which resets the enumerator to its initial position before the first element in the collection.
In summary, IEnumerable
is used to iterate over a collection of items, while IEnumerator
is used to get the current element from the collection.