CharEnumerator.MoveNext Method ()

 
System_CAPS_noteNote

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

Increments the internal index of the current CharEnumerator object to the next character of the enumerated string.

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

abstract MoveNext : unit -> bool
override MoveNext : unit -> bool

Return Value

Type: System.Boolean

true if the index is successfully incremented and within the enumerated string; otherwise, false.

The CharEnumerator class maintains an internal index to the enumerated string, and the MoveNext method increments the index by one. Call MoveNext after calling GetEnumerator or Reset to increment the current character position to the first character in the enumerated string. Check that the return value is true to determine that the current character position is valid.

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. MoveNext logically sets the index after the last (nonexistent) character of the enumerated string which is also invalid.

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
Show: