System.Collections.IEnumerable Interface

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Exposes the enumerator, which supports a simple iteration over a collection.
C# Syntax:
public interface IEnumerable
Remarks


Notes to implementors: IEnumerable must be implemented to support the ForEach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface.
See also:
System.Collections Namespace | IEnumerator

System.Collections.IEnumerable Member List:

Public Methods
GetEnumerator Returns an enumerator that can iterate through a collection.

System.Collections.IEnumerable Member Details

Method: GetEnumerator()
Summary
Returns an enumerator that can iterate through a collection.
C# Syntax:
IEnumerator GetEnumerator();
Return Value:
An IEnumerator that can be used to iterate through the collection.
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

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

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

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 can be invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset can throw an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current returns the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

See also:
IEnumerator

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.