[Serializable] |
There is no public constructor for CharEnumerator. Instead, call a String object's String.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 CharEnumerator.MoveNext method increments the index by one, so the first and subsequent characters are accessed in turn. The CharEnumerator.Reset method sets the index to a position logically before the first character. The CharEnumerator.Current property retrieves the character currently referenced by index. The CharEnumerator.Clone method creates a copy of the CharEnumerator.
Current | Read-only Gets the character in the enumerated string currently indexed by this instance. |
Clone | Creates a copy of this instance. |
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 | Increments the index to the next character of the enumerated string. |
Reset | Initializes the index to a position logically before the first character of the enumerated string. |
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 char Current {get;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The index is invalid; that is, it is before the first or after the last character of the enumerated string. |
The index is invalid after the String.GetEnumerator or CharEnumerator.Reset method is called. After either of these methods is called, invoke the CharEnumerator.MoveNext method to adjust the index to the first character in the enumerated string. The index is valid whenever the CharEnumerator.MoveNext method returns true.
CharEnumerator.Current does not move the index, and consecutive calls to CharEnumerator.Current return the same character until CharEnumerator.MoveNext, CharEnumerator.Reset, or String.GetEnumerator is called.
public object Clone(); |
For example, suppose your application uses an original instance of CharEnumerator to iterate through each character in a String. When some unique character is encountered, your application pauses processing and invokes the CharEnumerator.Clone method. In effect, this saves the CharEnumerator object's index in the String.
Your application uses the clone to navigate to another part of the String to perform some auxiliary processing. The side-effect of this navigation is the clone loses track of the position where processing stopped. However, when the auxiliary processing is complete, your application discards the clone and uses the original CharEnumerator instance to resume working on the String where the original processing stopped.
~CharEnumerator(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public bool MoveNext(); |
If the index is already beyond the last character of the enumerated string, the index is not changed and false is returned.
Notice that if the enumerated string is empty (""), the state of the CharEnumerator is always invalid. This is because the internal index for the CharEnumerator is initially before the first character of the enumerated string and is therefore invalid. CharEnumerator.MoveNext logically sets the index after the last (nonexistent) character of the enumerated string which is also invalid.
public void Reset(); |
public virtual string ToString(); |