C# | Get an enumerator that iterates through StringCollection Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 illustrate the use of above discussed method: Example 1: CSharp // C# code to get an StringEnumerator // that iterates through the StringCollection using System; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // Adding elements in StringCollection myCol.Add("A"); myCol.Add("B"); myCol.Add("C"); myCol.Add("D"); myCol.Add("E"); // taking an emumerator & // using GetEnumerator method StringEnumerator myenum = myCol.GetEnumerator(); // If MoveNext passes the end of the // collection, the enumerator is positioned // after the last element in the collection // and MoveNext returns false. while (myenum.MoveNext()) Console.WriteLine(myenum.Current); } } Output: A B C D E Example 2: CSharp // C# code to get an StringEnumerator // that iterates through the StringCollection using System; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // Adding elements in StringCollection myCol.Add("45"); myCol.Add("78"); myCol.Add("98"); myCol.Add("12"); myCol.Add("67"); // taking an emumerator // & using GetEnumerator method StringEnumerator myenum = myCol.GetEnumerator(); // If MoveNext passes the end of the // collection, the enumerator is positioned // after the last element in the collection // and MoveNext returns false. while (myenum.MoveNext()) Console.WriteLine(myenum.Current); } } Output: 45 78 98 12 67 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: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection.getenumerator?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get an enumerator that iterates through the ListDictionary K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-Namespace CSharp-Specialized-StringCollection +1 More Similar Reads C# | Get an enumerator that iterates through the stringDictionary 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 unders 3 min read C# | Get an enumerator that iterates through the stringDictionary 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 unders 3 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 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 Like