Export (0) Print
Expand All

IEnumerator Interface

Supports a simple iteration over a collection.

For a list of all members of this type, see IEnumerator Members.

[Visual Basic]
<Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")>
Public Interface IEnumerator
[C#]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
[C++]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public __gc __interface IEnumerator
[JScript]
public
   Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")
interface IEnumerator

Classes that Implement IEnumerator

Class Description
CharEnumerator Supports iterating over a String and reading its individual characters.
DbEnumerator Exposes the GetEnumerator method, which supports a simple iteration over a collection by a .NET Framework data provider.
ManagementObjectCollection.ManagementObjectEnumerator Represents the enumerator on the collection.
MessageEnumerator Provides a forward-only cursor to enumerate through messages in a message queue.
MessageQueueEnumerator Provides a forward-only cursor to enumerate through messages in a message queue.
MethodDataCollection.MethodDataEnumerator Represents the enumerator for MethodData objects in the MethodDataCollection.
PropertyDataCollection.PropertyDataEnumerator Represents the enumerator for PropertyData objects in the PropertyDataCollection.
QualifierDataCollection.QualifierDataEnumerator Represents the enumerator for QualifierData objects in the QualifierDataCollection.
SerializationInfoEnumerator Provides a formatter-friendly mechanism for parsing the data in SerializationInfo. This class cannot be inherited.
TextElementEnumerator Enumerates the text elements of a string.
X509CertificateCollection.X509CertificateEnumerator Enumerates the X509Certificate objects in an X509CertificateCollection.
XmlSchemaCollectionEnumerator Supports a simple iteration over a collection. This class cannot be inherited.
XmlSchemaObjectEnumerator Represents the enumerator for the XmlSchemaObjectCollection. This class cannot be inherited.

Remarks

IEnumerator is the base interface for all enumerators.

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. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. 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 either MoveNext or Reset is called. MoveNext sets 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 MoveNext returns false. If the last call to MoveNext returned false, calling Current throws an exception. To set Current to the first element of the collection again, you can call Reset followed by 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 is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current will return 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.

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

IEnumerator Members | System.Collections Namespace | IEnumerable | ICollection

Show:
© 2014 Microsoft