Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

Tags:

People also ask

What's the difference between IEnumerable and IEnumerator?

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 .

What is IEnumerable T in C#?

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.

What is difference between IEnumerable and enumerable in C#?

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.

What is GetEnumerator?

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?