Dela via


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 requires 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 property moves the enumerator back to the beginning of the collection. Current is positioned before the first element. Reset is not available in the generic IEnumerator<T> interface.

Behavior of an Enumerator

Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. 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.

In non-generic collections, you can call Reset followed by MoveNext to move the enumerator back to the beginning of the collection.

In generic collections, you cannot set Current to the first element of the collection again; you must create a new enumerator instance instead.

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