In this blog we can see what is IEnumerable in C# .Net.

What is IEnumerable in C#?

In C#, the interface IEnumerable defines the method GetEnumerator, which produces an IEnumerator interface as a result. A collection that implements IEnumerable can therefore be used with a for-each statement if this enables read-only access to it.

Key Points :

  1. The System.Collections are contained in the IEnumerable interface.Common namespace.
  2. A generic interface called IEnumerable enables looping over generic and non-generic lists.
  3. Linq query expressions can also be used using the IEnumerable interface.
  4. Interface IEnumerable gives back an iterator that goes through the collection of items.

Example:

public class Customer : IEnumerable {

public IEnumerator GetEnumerator() {

throw new NotImplementedException(); 

}

 }

What is IEnumerator in C# .Net?

An interface called IEnumerator makes it easier to retrieve the collection’s most recent elements.

IEnumerator Contain 2 methods 

  1. MoveNext()
  2.  Reset()

MoveNext() :

Sets the enumerator to the collection’s next element; it returns true if the operation was successful and false if the enumerator has reached the collection’s end.

Reset() :

returns the enumerator to its starting place, which is in front of the collection’s first element.

Example :

public class Customer : IEnumerator
{
public object Current
{
get { throw new NotImplementedException(); }
}

public bool MoveNext()
{
throw new NotImplementedException();
}

public void Reset()
{
throw new NotImplementedException();
}
}

For any Help or Queries Contact us on info@crmonce.com or +918096556344