
Some C# collection types offer the GetEnumerator method. This method returns an Enumerator object that can be used to loop through the collection. Complete knowledge of the actual collection type is not necessary. It becomes possible to write methods that act on the IEnumerator interface.
Tip: IEnumerator is not the same as IEnumerable. IEnumerator is an interface implemented by Enumerator objects. IEnumerable is a collection that can be looped over with foreach loops.
This program demonstrates how the GetEnumerator() method on the List type works. On a List<int>, GetEnumerator returns a List<int>.Enumerator object. This object implements IEnumerator<int>. We can then write methods that receive IEnumerator<int>.
Program that uses GetEnumerator [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(9);
List<int>.Enumerator e = list.GetEnumerator();
Write(e);
}
static void Write(IEnumerator<int> e)
{
while (e.MoveNext())
{
int value = e.Current;
Console.WriteLine(value);
}
}
}
Output
1
5
9Other collection types also provide GetEnumerator. For example, LinkedList returns LinkedList<int>.Enumerator. This object too can be passed to the Write() method in the example above. We only have to have one Write() method to handle all generic enumerators that handle integers.

The GetEnumerator method is an instance method provided on several different collection types. It can help in the development of abstractions in programs. A unified method can be used to loop over the Enumerator returned by all these collections.
Dictionary GetEnumerator Collections