Export (0) Print
Expand All

CharEnumerator Class

Supports iterating over a String object and reading its individual characters. This class cannot be inherited.

System.Object
  System.CharEnumerator

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[<Sealed>]
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type CharEnumerator =  
    class 
        interface ICloneable 
        interface IEnumerator<char>
        interface IEnumerator 
        interface IDisposable 
    end

The CharEnumerator type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkCurrentGets the currently referenced character in the string enumerated by this CharEnumerator object.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCloneCreates a copy of the current CharEnumerator object.
Public methodDisposeReleases all resources used by the current instance of the CharEnumerator class.
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkMoveNextIncrements the internal index of the current CharEnumerator object to the next character of the enumerated string.
Public methodSupported by the XNA FrameworkResetInitializes the index to a position logically before the first character of the enumerated string.
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIDisposable.DisposeReleases all resources used by the CharEnumerator class.
Explicit interface implemetationPrivate propertySupported by the XNA FrameworkIEnumerator.CurrentInfrastructure. Gets the currently referenced character in the string enumerated by this CharEnumerator object. For a description of this member, see IEnumerator.Current.
Top

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.

NoteNote

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft