Open In App

C# | Get an enumerator that iterates through StringCollection

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
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:




// 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:




// 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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads