System.Collections.Specialized.StringEnumerator Class

Assembly: System.dll
Namespace: System.Collections.Specialized
Summary
Supports a simple iteration over a StringCollection.
C# Syntax:
public class StringEnumerator
Thread Safety
Public static (non-instance) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.
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. 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.

See also:
System.Collections.Specialized Namespace | IEnumerable | IEnumerator | StringCollection

System.Collections.Specialized.StringEnumerator Member List:

Public Properties
Current Read-only

Gets the current element in the collection.
Public Methods
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.
Protected Methods
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:


System.Collections.Specialized.StringEnumerator Member Details

Property: Current (read-only)
Summary
Gets the current element in the collection.
C# Syntax:
public string Current {get;}
Exceptions
Exception Type Condition
InvalidOperationException The enumerator is positioned before the first element of the collection or after the last element.
Remarks
After an enumerator is created or after a StringEnumerator.Reset, StringEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of StringEnumerator.Current; otherwise, StringEnumerator.Current is undefined.

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.

See also:
StringEnumerator.MoveNext | StringEnumerator.Reset | IEnumerator.Current

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~StringEnumerator();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MoveNext()
Summary
Advances the enumerator to the next element of the collection.
C# Syntax:
public bool MoveNext();
Return Value:
true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
Exceptions
Exception Type Condition
InvalidOperationException The collection was modified after the enumerator was created.
Remarks
After an enumerator is created or after a call to StringEnumerator.Reset, an enumerator is positioned before the first element of the collection, and the first call to StringEnumerator.MoveNext moves the enumerator over the first element of the collection.

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.

See also:
StringEnumerator.Current | StringEnumerator.Reset | IEnumerator.MoveNext

Return to top


Method: Reset()
Summary
Sets the enumerator to its initial position, which is before the first element in the collection.
C# Syntax:
public void Reset();
Exceptions
Exception Type Condition
InvalidOperationException The collection was modified after the enumerator was created.
Remarks
StringEnumerator.Reset moves the enumerator to the beginning of the collection, before the first element. After StringEnumerator.Reset, StringEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of StringEnumerator.Current.
See also:
StringEnumerator.MoveNext | StringEnumerator.Current | IEnumerator.Reset

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

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