C# | Get an enumerator that iterates through the stringDictionary Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report StringDictionary.GetEnumerator method is used to return an enumerator that iterates through the string dictionary. Syntax: public virtual System.Collections.IEnumerator GetEnumerator (); Return Value: An IEnumerator that iterates through the string dictionary. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get an enumerator // that iterates through the stringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); // "IEnumerator" interface supports a simple // iteration over a non-generic collection. IEnumerator myEnumerator = myDict.GetEnumerator(); DictionaryEntry de; // "MoveNext" advances the enumerator // to the next element of the collection. // you must call "MoveNext" to advance the // enumerator to the first element of the // collection before reading the value of "Current" while (myEnumerator.MoveNext()) { // "Current" returns the same object until // either "MoveNext" is called. // "MoveNext" sets "Current" to the next element. de = (DictionaryEntry)myEnumerator.Current; Console.WriteLine(de.Key + " " + de.Value); } } } Output: d Dog b Banana c Cat a Apple Example 2: CSHARP // C# code to get an enumerator // that iterates through the stringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("I", "one"); myDict.Add("II", "two"); myDict.Add("III", "three"); myDict.Add("IV", "four"); myDict.Add("V", "five"); // "IEnumerator" interface supports a simple // iteration over a non-generic collection. IEnumerator myEnumerator = myDict.GetEnumerator(); DictionaryEntry de; // "MoveNext" advances the enumerator // to the next element of the collection. // you must call "MoveNext" to advance the // enumerator to the first element of the // collection before reading the value of "Current" while (myEnumerator.MoveNext()) { // "Current" returns the same object until // either "MoveNext" is called. // "MoveNext" sets "Current" to the next element. de = (DictionaryEntry)myEnumerator.Current; Console.WriteLine(de.Key + " " + de.Value); } } } Output: iv four i one iii three v five ii two Note: Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection. 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: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.getenumerator?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get an enumerator that iterates through the ListDictionary S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace Similar Reads C# | Get an enumerator that iterates through the ListDictionary ListDictionary.GetEnumerator method is used to returns an IDictionaryEnumerator that iterates through the ListDictionary. Syntax: public System.Collections.IDictionaryEnumerator GetEnumerator (); Return Value: This method returns an IDictionaryEnumerator for the ListDictionary. Below programs illust 2 min read C# | Get an enumerator that iterates through the ListDictionary ListDictionary.GetEnumerator method is used to returns an IDictionaryEnumerator that iterates through the ListDictionary. Syntax: public System.Collections.IDictionaryEnumerator GetEnumerator (); Return Value: This method returns an IDictionaryEnumerator for the ListDictionary. Below programs illust 2 min read C# | Get an enumerator that iterates through the SortedDictionary 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 Sort 3 min read C# | Get an enumerator that iterates through the SortedDictionary 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 Sort 3 min read C# | Get an enumerator that iterates through StringCollection StringCollection.GetEnumerator Method is used to get a StringEnumerator that iterates through the StringCollection. Syntax: public System.Collections.Specialized.StringEnumerator GetEnumerator (); Return Value: This method returns a StringEnumerator for the StringCollection. Below programs illustrat 2 min read C# | Get an enumerator that iterates through StringCollection StringCollection.GetEnumerator Method is used to get a StringEnumerator that iterates through the StringCollection. Syntax: public System.Collections.Specialized.StringEnumerator GetEnumerator (); Return Value: This method returns a StringEnumerator for the StringCollection. Below programs illustrat 2 min read Like