VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-get-an-enumerator-that-iterates-through-the-sorteddictionary/

⇱ C# | Get an enumerator that iterates through the SortedDictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Get an enumerator that iterates through the SortedDictionary

Last Updated : 11 Jul, 2025
SortedDictionary<TKey, TValue>.GetEnumerator Method is used to get an enumerator that iterates through the SortedDictionary<TKey, TValue>. Syntax:
public System.Collections.Generic.SortedDictionary<TKey, TValue>.Enumerator GetEnumerator ();
Return Value: This method returns an SortedDictionary<TKey, TValue>.Enumerator for the SortedDictionary<TKey, TValue>. Below programs illustrate the above-discussed method: Below programs illustrate thabove-discusseddiscussed method: Example 1:
Output:
Australia --> Canberra
Belgium --> Brussels
China --> Beijing
India --> New Delhi
Netherlands --> Amsterdam
Russia --> Moscow
Example 2:
Output:
I --> C
II --> C++
III --> Java
IV --> Python
V --> C#
Note:
  • The foreach statement of the C# language hides the complexity of the enumerators. Therefore, using foreach is recommended, instead of directly manipulating the enumerator.
  • Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
  • Current returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.
  • An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.
  • This method is an O(1) operation.
Reference:
Comment

Explore