Get an enumerator that iterates through the SortedSet in C#

The SortedSet<T> class in C# provides the GetEnumerator() method to retrieve an enumerator that iterates through the collection in sorted order. This enumerator allows you to manually control the iteration process using MoveNext() and Current properties.

Syntax

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

SortedSet<T>.Enumerator enumerator = sortedSet.GetEnumerator();

Following is the syntax for using the enumerator to iterate −

while (enumerator.MoveNext()) {
   T currentElement = enumerator.Current;
   // process currentElement
}

Using GetEnumerator() with String Elements

The following example demonstrates how to use GetEnumerator() with a SortedSet of strings −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      Console.WriteLine("Elements in SortedSet1 (using foreach)...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2 (using GetEnumerator)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Elements in SortedSet1 (using foreach)...
AB
BC
CD
EF
Elements in SortedSet2 (using GetEnumerator)...
AB
BC
CD
DE
EF
HI
JK

Using GetEnumerator() with Integer Elements

The following example shows GetEnumerator() working with numeric data −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(100);
      set1.Add(200);
      set1.Add(300);
      set1.Add(400);
      set1.Add(500);
      set1.Add(600);
      set1.Add(700);
      Console.WriteLine("Elements in SortedSet (using GetEnumerator)...");
      SortedSet<int>.Enumerator demoEnum = set1.GetEnumerator();
      while (demoEnum.MoveNext()) {
         int res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Elements in SortedSet (using GetEnumerator)...
100
200
300
400
500
600
700

Comparison: foreach vs GetEnumerator()

foreach Loop GetEnumerator()
Simpler syntax and more readable More control over iteration process
Automatically handles MoveNext() and Current Manual control of MoveNext() and Current
Cannot pause or resume iteration easily Can pause, store state, and resume later
Preferred for simple iterations Useful for complex iteration logic

Conclusion

The GetEnumerator() method provides manual control over SortedSet iteration using MoveNext() and Current. While foreach loops are simpler for basic iteration, GetEnumerator() offers more control when you need to pause, resume, or implement custom iteration logic.

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

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements