IEnumerable.md - brainchildservices/curriculum GitHub Wiki

IEnumerable in C#

IEnumerable in C# is an interface that is a parent for all the collection class.

image

 namespace System.Collections
 {
      public interface IEnumerable
      {
           IEnumerator GetEnumerator();
      }
 }

Slide 2

What is the importance of IEnumerable interface?

We are able to iterate over a List, Array, etc using the forach loop.

What makes it possible to iterate over the array?

It is a method called GetEnumerator, defied in the IEnumerable interface.
Each of the collections (IList, IDictionary) that are inheriting from the IEnumerable is supposed to have the GetEnumerator method implemented. It defines one method, GetEnumerator which returns an IEnumerator interface.

In Visual Studio create a List. Right click and click on "Go to Defintion". We could see that the class List inherits the IEnumerable interface.
Right click on IEnumerable and click on "Go to Defintion".
Here we could see the IEnumerable which has only one method GetEnumerator() with return type IEnumerator. .

Slide 3

Key Points

  • IEnumerable interface contains the System.Collections.Generic namespace.
  • IEnumerable interface is a generic interface that allows looping over generic or non-generic lists.
  • IEnumerable interface Returns an enumerator that iterates through the collection.

Slide 4

IEnumerator

The enumerator is an object that can return each item in a collection, one by one,in order, as they’re requested. The enumerator “knows” the order of the items and keeps track of where it is in the sequence. It then returns the current item when it is requested.

Right-click and go to IEnumerator and click on "Go to Defintion".

 public interface IEnumerator
 {
      object Current { get; }

      bool MoveNext();
      void Reset();
 }

Slide 5

It has two methods called MoveNext and Reset. It has a property called Current.

  • Current returns the item at the current position in the sequence.

    • It is a read-only property.
    • It returns a reference of type object, so an object of any type can be returned.
  • MoveNext is a method that advances the enumerator’s position to the next item in the collection. It also returns a Boolean value, indicating whether the new position is a valid position or is beyond the end of the sequence.

    • If the new position is valid, the method returns true.
    • If the new position isn’t valid (that is, the current position is beyond the end),the method returns false.
  • Reset is a method that resets the position to the initial state.

Slide 6

IEnumerable vs IEnumerator interface

  • IEnumerable and IEnumerator are both interfaces.
  • IEnumerable has just one method called GetEnumerator. This method returns another type which is an interface that interface is IEnumerator.
  • If we want to implement enumerator logic in any collection class, it needs to implement IEnumerable interface (either generic or non-generic) .
  • IEnumerable has just one method whereas IEnumerator has two methods (MoveNext and Reset) and a property Current.

References:
https://www.youtube.com/watch?v=zyhGO4GXTQI
http://www.differencebetween.net/technology/difference-between-ienumerable-and-ienumerator/
https://www.csharpstar.com/difference-between-ienumerator-and-ienumerable-interface-csharp/
https://www.c-sharpcorner.com/UploadFile/0c1bb2/ienumerable-interface-in-C-Sharp/