Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class CharEnumerator : ICloneable, IEnumerator<char>, IDisposable, IEnumerator
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public final class CharEnumerator implements ICloneable, IEnumerator<char>, IDisposable, IEnumerator
SerializableAttribute ComVisibleAttribute(true) public final class CharEnumerator implements ICloneable, IEnumerator<char>, IDisposable, IEnumerator
A CharEnumerator provides read-only access to the characters in a referenced String object. For example, the foreach statement of the Microsoft Visual Basic and C# programming languages, which iterates through the elements of a collection, retrieves a CharEnumerator from a String object in order to iterate through the characters in that object.
There is no public constructor for CharEnumerator. Instead, call a String object's GetEnumerator method to obtain a CharEnumerator that is initialized to reference the string.
A CharEnumerator maintains an internal index to the characters in the string the CharEnumerator references. The state of the index is invalid when it references a character position logically before the first character or after the last character in the string, and valid when it references a character within the string. The index is initialized to a position logically before the first character, and is set to a position after the last character when the iteration is complete. An exception is thrown if you attempt to access a character while the index is invalid.
The MoveNext method increments the index by one, so the first and subsequent characters are accessed in turn. The Reset method sets the index to a position logically before the first character. The Current property retrieves the character currently referenced by index. The Clone method creates a copy of the CharEnumerator.
Several independent instances of CharEnumerator across one or more threads can have access to a single instance of String. This class is implemented to support the IEnumerator interface. For more information regarding the use of an enumerator, see the IEnumerator topic.
The following example uses the CharEnumerator class to enumerate the individual characters in a string. It instantiates a CharEnumerator object by calling the String.GetEnumerator method, moves from one character to the next by calling the MoveNext method, and displays the current character by retrieving the value of the Current property.
Note, however, that the same operation can be performed somewhat more intuitively by using foreach (in C#) or For Each (in Visual Basic), as the following example shows.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.