Getting an enumerator that iterates through HashSet in C#

A HashSet in C# is a collection that stores unique elements without duplicates. To iterate through a HashSet, you can use the GetEnumerator() method which returns an enumerator object, or use a foreach loop for simpler iteration.

The HashSet<T>.Enumerator provides manual control over iteration using MoveNext() and Current properties, while foreach handles enumeration automatically behind the scenes.

Syntax

Following is the syntax for getting an enumerator from a HashSet −

HashSet<T>.Enumerator enumerator = hashSet.GetEnumerator();
while (enumerator.MoveNext()) {
    T current = enumerator.Current;
    // use current element
}

Alternatively, using foreach for automatic enumeration −

foreach (T element in hashSet) {
    // use element
}

Using GetEnumerator() Method

The GetEnumerator() method returns a HashSet<T>.Enumerator struct that implements the enumeration pattern with MoveNext() and Current

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(String[] args) {
        HashSet<string> set1 = new HashSet<string>();
        set1.Add("A");
        set1.Add("B");
        set1.Add("C");
        set1.Add("D");
        set1.Add("E");
        set1.Add("F");
        set1.Add("G");
        set1.Add("H");
        Console.WriteLine("Elements in HashSet1...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }
        HashSet<string> set2 = new HashSet<string>();
        set2.Add("John");
        set2.Add("Jacob");
        set2.Add("Ryan");
        set2.Add("Tom");
        set2.Add("Andy");
        set2.Add("Tim");
        set2.Add("Steve");
        set2.Add("Mark");
        Console.WriteLine("Elements in HashSet2... (Enumerator iterating through HashSet)");
        HashSet<string>.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext()) {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }
        Console.WriteLine("Is HashSet1 equal to HashSet2? = "+set1.Equals(set2));
        Console.WriteLine("Count of HashSet2 = "+set2.Count);
        set2.Clear();
        Console.WriteLine("Count of HashSet2 (updated) = "+set2.Count);
    }
}

The output of the above code is −

Elements in HashSet1...
A
B
C
D
E
F
G
H
Elements in HashSet2... (Enumerator iterating through HashSet)
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
Is HashSet1 equal to HashSet2? = False
Count of HashSet2 = 8
Count of HashSet2 (updated) = 0

HashSet with Duplicate Elements

HashSet automatically eliminates duplicate elements. When you add the same element multiple times, it stores only one copy −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        HashSet<string> set1 = new HashSet<string>();
        set1.Add("AB");
        set1.Add("CD");
        set1.Add("EF");
        set1.Add("AB");
        set1.Add("IJ");
        set1.Add("KL");
        set1.Add("EF");
        set1.Add("OP");
        Console.WriteLine("Elements in HashSet1");
        foreach(string val in set1) {
            Console.WriteLine(val);
        }
        HashSet<string> set2 = new HashSet<string>();
        set2.Add("EF");
        set2.Add("KL");
        Console.WriteLine("Elements in HashSet2... (Enumerator iterating through HashSet)");
        HashSet<string>.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext()) {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }
        Console.WriteLine("Is set1 a superset of set2? "+set1.IsSupersetOf(set2));
    }
}

The output of the above code is −

Elements in HashSet1
AB
CD
EF
IJ
KL
OP
Elements in HashSet2... (Enumerator iterating through HashSet)
EF
KL
Is set1 a superset of set2? True

Comparison: GetEnumerator() vs foreach

GetEnumerator() Method foreach Loop
Manual control over iteration Automatic iteration
Requires MoveNext() and Current Handles enumeration internally
More verbose syntax Cleaner, more readable syntax
Useful for custom iteration logic Best for simple iteration scenarios

Conclusion

The GetEnumerator() method provides manual control over HashSet iteration using MoveNext() and Current, while foreach offers a simpler approach. HashSet automatically handles duplicate elements by storing only unique values, making it ideal for collections where uniqueness is required.

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

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements