Get an enumerator that iterates through Collection in C#

To get an enumerator that iterates through a Collection in C#, you use the GetEnumerator() method. An enumerator provides a way to iterate through collection elements one by one using MoveNext() and Current properties.

The Collection<T> class is part of the System.Collections.ObjectModel namespace and provides a generic collection that can be customized by inheritance.

Syntax

Following is the syntax for getting and using an enumerator −

var enumerator = collection.GetEnumerator();
while (enumerator.MoveNext()) {
   // Access current element using enumerator.Current
}

How It Works

  • GetEnumerator() returns an IEnumerator<T> object

  • MoveNext() advances to the next element and returns true if successful

  • Current property gets the element at the current position

  • The enumerator starts before the first element, so MoveNext() must be called first

Enumerator Flow Collection GetEnumerator() returns IEnumerator MoveNext() true/false Current Start: Before first element Get iterator object Advance & validate Access element Repeat MoveNext() ? Current until MoveNext() returns false

Using Enumerator with String Collection

Example

using System;
using System.Collections.ObjectModel;

public class Demo {
   public static void Main() {
      Collection<string> col = new Collection<string>();
      col.Add("Andy");
      col.Add("Kevin");
      col.Add("John");
      col.Add("Kevin");
      col.Add("Mary");
      col.Add("Katie");
      col.Add("Barry");
      col.Add("Nathan");
      col.Add("Mark");
      
      Console.WriteLine("Count of elements = " + col.Count);
      Console.WriteLine("Iterating through the collection...");
      
      var enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }
   }
}

The output of the above code is −

Count of elements = 9
Iterating through the collection...
Andy
Kevin
John
Kevin
Mary
Katie
Barry
Nathan
Mark

Using Enumerator with Integer Collection

Example

using System;
using System.Collections.ObjectModel;

public class Demo {
   public static void Main() {
      Collection<int> col = new Collection<int>();
      col.Add(100);
      col.Add(200);
      col.Add(300);
      col.Add(400);
      col.Add(500);
      
      Console.WriteLine("Count of elements = " + col.Count);
      Console.WriteLine("Iterating through the collection...");
      
      var enumerator = col.GetEnumerator();
      while (enumerator.MoveNext()) {
         Console.WriteLine(enumerator.Current);
      }
   }
}

The output of the above code is −

Count of elements = 5
Iterating through the collection...
100
200
300
400
500

Enumerator vs foreach Loop

Enumerator foreach Loop
Manual control with MoveNext() and Current Automatic iteration handled by compiler
More verbose but offers fine-grained control Cleaner syntax, less code
Can pause iteration and resume later Must complete the entire iteration
Useful for custom iteration logic Best for simple, complete iterations

Conclusion

The GetEnumerator() method provides manual control over collection iteration using MoveNext() and Current. While foreach loops are more convenient for simple iterations, enumerators offer fine-grained control when you need custom iteration logic or want to pause and resume iteration.

Updated on: 2026-03-17T07:04:36+05:30

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements