CharEnumerator.Current Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets the currently referenced character in the string enumerated by this CharEnumerator object.

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

abstract Current : char with get
override Current : char with get

Property Value

Type: System.Char

The Unicode character currently referenced by this CharEnumerator object.

Exception Condition

The index is invalid; that is, it is before the first or after the last character of the enumerated string.

The CharEnumerator class maintains an internal index to the enumerated string, and the Current property returns the character that is currently referenced by the index. This property should be invoked only when the index is valid; otherwise, an exception is thrown.

The index is always invalid for an empty string (""). The index is also invalid after the String.GetEnumerator or Reset method is called. After either of these methods is called, invoke the MoveNext method to adjust the index to the first character in the enumerated string. The index is valid whenever the MoveNext method returns true.

Current does not move the index, and consecutive calls to Current return the same character until MoveNext, Reset, or String.GetEnumerator is called.

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
Available since 1.1
Return to top