IEnumerable vs IEnumerator in C#



In C#, IEnumerable and IEnumerator are fundamental interfaces used for iterating over collections. They provide a standard way to go through (traverse) each element without revealing how the collection is built or stored internally.

IEnumerable in C#

In C#, IEnumerable is an interface representing a collection of objects that can be iterated over.

  • It allows you to traverse through a collection without revealing how the collection is built or stored internally.
  • IEnumerable exists in two forms: non-generic (System.Collections Namespace) and generic (System.Collections.Generic Namespace).
In C#, namespaces are folders that contain and organize classes, interfaces, and other types.

Non-generic IEnumerable in C#

Non-generic IEnumerable comes under the "System.Collections" namespace and represents a collection of objects of any type, so you don't need to specify the element type.

Let's understand non-generic IEnumerable by a C# example.

using System;
using System.Collections;

class Program {
   static void Main() {
   
      // can store any type of objects
      IEnumerable collection = new ArrayList();
      collection = new ArrayList { 1, "Hello", 3.5 };

      foreach (var item in collection) {
         Console.WriteLine(item);
      }
   }
}

When you run this code, it will produce the following output

1
Hello
3.5

Generic IEnumerable in C#

Generic IEnumerable comes under the "System.Collections.Generic" namespace and represents a collection of objects of specific type (strongly typed), so you need to specify the element type.

To specify the type use <T> (like <int> or <string>), which prevents type errors and improves performance.

The following example demonstrates how to use generic IEnumerable in C#

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4 };

      foreach (int number in numbers) {
         Console.WriteLine(number);
      }
   }
}

Following is the output

1
2
3
4

Here, the collection can only store integers, which is safer and faster.

IEnumerator in C#

In C#, IEnumerator is an interface that actually performs the iteration over a collection and responsible for managing the current position within the collection during iteration.

  • It provides methods to move through the collection and access the current element.
  • IEnumerator also exists in two forms: non-generic (System.Collections Namespace) and generic (System.Collections.Generic Namespace).

Following are the three members of the IEnumerator −

  • Current property − It return the elements at the current position of the enumerator.
  • MoveNext() method − Moves the enumerator to the next element and returns true if the enumerator successfully moves; otherwise, it returns false.
  • Reset() method − Reset enumerator to its initial position.

Non-generic IEnumerator in C#

A non-generic IEnumerator iterates over an IEnumerable collection that can contain objects of any type. The following example shows how it works −

using System;
using System.Collections;
using System.Collections.Generic;

class Program {
   static void Main() {
      // Using ArrayList because it can store objects of any type
      IEnumerable collection = new ArrayList { 1, "Hello", 3.5, 'T', "Aman" };

      IEnumerator enumerator = collection.GetEnumerator();

      // MoveNext() moves to the next element
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }

      // Optional: to start over
      enumerator.Reset();
   }
}

Following is the output

1
Hello
3.5
T
Aman

Generic IEnumerator in C#

The IEnumerator iterates over an IEnumerable collection that only contains objects of a specific type. See the following example to learn how it works −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
      
      IEnumerator<int> enumerator = numbers.GetEnumerator();
      // MoveNext method of Enumerator
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }
      // optional, to start over
      enumerator.Reset();
   }
}

When you run this code, it will produce the following output

1
2
3
4
5

Explanation: IEnumerator provides manual control of iteration. So, we have to explicitly call "MoveNext()" and "Current" to move to the next element.

Conclusion

IEnumerable and IEnumerator are used to loop through collections in C#. IEnumerable provides a way to get the items from a collection one by one using a foreach loop. IEnumerator is used to move through the collection and get each item during the loop.

Advertisements