public class StringEnumerator
|
Initially, the enumerator is positioned before the first element in the collection. StringEnumerator.Reset also brings the enumerator back to this position. At this position, calling StringEnumerator.Current throws an exception. Therefore, you must call StringEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of StringEnumerator.Current.
StringEnumerator.Current returns the same object until either StringEnumerator.MoveNext or StringEnumerator.Reset is called. StringEnumerator.MoveNext sets StringEnumerator.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 StringEnumerator.MoveNext returns false. If the last call to StringEnumerator.MoveNext returned false, calling StringEnumerator.Current throws an exception. To set StringEnumerator.Current to the first element of the collection again, you can call StringEnumerator.Reset followed by StringEnumerator.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 StringEnumerator.MoveNext or StringEnumerator.Reset throws an InvalidOperationException. If the collection is modified between StringEnumerator.MoveNext and StringEnumerator.Current, StringEnumerator.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.
Current | Read-only Gets the current element in the collection. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
MoveNext | Advances the enumerator to the next element of the collection. |
Reset | Sets the enumerator to its initial position, which is before the first element in the collection. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public string Current {get;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The enumerator is positioned before the first element of the collection or after the last element. |
StringEnumerator.Current also throws an exception if the last call to StringEnumerator.MoveNext returned false, which indicates the end of the collection.
StringEnumerator.Current does not move the position of the enumerator and consecutive calls to StringEnumerator.Current return the same object until either StringEnumerator.MoveNext or StringEnumerator.Reset is called.
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 IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
~StringEnumerator(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public bool MoveNext(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The collection was modified after the enumerator was created. |
After the end of the collection is passed, subsequent calls to StringEnumerator.MoveNext return false until StringEnumerator.Reset is called.
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 IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
public void Reset(); |
Exception Type | Condition |
---|---|
InvalidOperationException | The collection was modified after the enumerator was created. |
public virtual string ToString(); |