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 illustrate the use of above discussed method:
Example 1:
// 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); } } |
chevron_right
filter_none
Output:
A B C D E
Example 2:
// 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); } } |
chevron_right
filter_none
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:
Recommended Posts:
- C# | Get an enumerator that iterates through the SortedSet
- C# | Enumerator that iterates through the BitArray
- C# | Get an enumerator that iterates through the Dictionary
- C# | Get an enumerator that iterates through Collection<T>
- C# | Get an enumerator that iterates through the SortedList
- C# | Get an enumerator that iterates through the stringDictionary
- C# | Get an enumerator that iterates through the Hashtable
- C# | Get an enumerator that iterates through the List
- C# | Get an enumerator that iterates through the ListDictionary
- C# | Getting an enumerator that iterates through LinkedList<T>
- C# | Get an enumerator that iterates through the HybridDictionary
- C# | Getting an enumerator that iterates through HashSet<T>
- Getting enumerator that iterates through the Queue in C#
- C# | Get an enumerator that iterates through the SortedDictionary
- Getting an enumerator that iterates through the Stack in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



