Open In App

C# | Get an enumerator that iterates through Collection<T>

Improve
Improve
Like Article
Like
Save
Share
Report

Collection<T>.GetEnumerator Method is used to get an enumerator that iterates through the Collection<T>.

Syntax:

public System.Collections.Generic.IEnumerator<T> GetEnumerator ();

Return Value: This method returns an IEnumerator<T> for the Collection<T>.

Below programs illustrate the use of above discussed method:

Example 1:




// C# code to get an Enumerator that
// iterates through the Collection<T>
using System;
using System.Collections.ObjectModel;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a collection of strings
        Collection<string> myColl = new Collection<string>();
  
        myColl.Add("A");
        myColl.Add("B");
        myColl.Add("C");
        myColl.Add("D");
        myColl.Add("E");
  
        // Displaying the number of elements in Collection
        Console.WriteLine("The number of elements in myColl are: "
                                                  + myColl.Count);
  
        // To get an Enumerator
        // for the Collection
        var enumerator = myColl.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 (enumerator.MoveNext()) {
  
            Console.WriteLine(enumerator.Current);
        }
    }
}


Output:

The number of elements in myColl are: 5
A
B
C
D
E

Example 2:




// C# code to get an Enumerator that
// iterates through the Collection<T>
using System;
using System.Collections.ObjectModel;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a collection of integers
        Collection<int> myColl = new Collection<int>();
  
        myColl.Add(45);
        myColl.Add(56);
        myColl.Add(78);
        myColl.Add(75);
  
        // Displaying the number of elements in Collection
        Console.WriteLine("The number of elements in myColl are: "
                                                  + myColl.Count);
  
        // To get an Enumerator
        // for the Collection
        var enumerator = myColl.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 (enumerator.MoveNext()) {
  
            Console.WriteLine(enumerator.Current);
        }
    }
}


Output:

The number of elements in myColl are: 4
45
56
78
75

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:



Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads