Enumerating a Collection

The .NET Framework provides enumerators as an easy way to iterate through a collection. Enumerators only read data in the collection; they cannot be used to modify the underlying collection.

Some languages provide a statement that hides the complexity of using enumerators directly. The C# foreach statement, the C++ for each statement, and the Visual Basic For Each statement use enumerators.

About Enumerators

An enumerator flattens a collection so that the members can be accessed sequentially. Different collection classes might have different sequences. For example, an enumerator for an ArrayList preserves the order in which the elements are entered in the collection, whereas an enumerator for a Hashtable displays the elements according to the hash code of the element.

Every enumerator is based on the IEnumerator interface or the IEnumerator<T> generic interface, which contains the following members:

  • The Current property points to the current member in the collection.

  • The MoveNext property moves the enumerator to the next member in the collection.

  • The Reset method moves the enumerator back to the beginning of the collection, which means Current is positioned before the first element. The Reset method is provided for COM interoperability and does not need to be fully implemented; instead, the implementer can simply throw a NotSupportedException.

Behavior of an Enumerator

Initially, the enumerator is positioned before the first element in the collection. At this position, Current is undefined. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns the same object until MoveNext or Reset is called. MoveNext sets Current to the next element.

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, Current is undefined.

If it’s implemented, you can call the Reset followed by MoveNext to move the enumerator back to the beginning of the collection. If Reset is not implemented, you must create a new enumerator instance to return to the first element of the 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.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization or use one of the thread-safe collection classes in the System.Collections.Concurrent namespace. The System.Collections.Concurrent.ConcurrentQueue<T> and System.Collections.Concurrent.ConcurrentStack<T> classes take a snapshot of the elements before enumerating them, to prevent mutations to the collection on another thread. The System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> class does not take a snapshot.

The System.Collections.Concurrent.BlockingCollection<T> class provides an enumerator method called GetConsumingEnumerable that mutates the collection by removing the items from the collection as it enumerates them.

See Also

Reference

IEnumerator

IEnumerator<T>

IDictionaryEnumerator

IEnumerable

IEnumerable<T>

Other Resources

Creating and Manipulating Collections

Thread-Safe Collections