CharEnumerator.MoveNext() Method in C#

The CharEnumerator.MoveNext() method in C# advances the internal index of the current CharEnumerator object to the next character of the enumerated string. It returns true if the enumerator successfully moved to the next character, or false if it has passed the end of the string.

Syntax

public bool MoveNext();

Return Value

The method returns a bool value −

  • true − if the enumerator was successfully advanced to the next character

  • false − if the enumerator has passed the end of the string

CharEnumerator.MoveNext() Flow Start Before first MoveNext() returns true/false Current character Use in while loop to iterate through all characters

Example

Let us see an example to implement the CharEnumerator.MoveNext() method −

using System;

public class Demo {
   public static void Main() {
      string strNum = "john";
      CharEnumerator ch = strNum.GetEnumerator();
      Console.WriteLine("HashCode = " + ch.GetHashCode());
      Console.WriteLine("Get the Type = " + ch.GetType());
      
      while (ch.MoveNext()) {
         Console.Write(ch.Current + " ");
      }
      Console.WriteLine();
      
      // disposed
      ch.Dispose();
   }
}

The output of the above code is −

HashCode = 1373506
Get the Type = System.CharEnumerator
j o h n 

Using MoveNext() with Reset()

You can reset the enumerator to iterate through the string again −

using System;

public class Demo {
   public static void Main() {
      string text = "Hello";
      CharEnumerator enumerator = text.GetEnumerator();
      
      Console.WriteLine("First iteration:");
      while (enumerator.MoveNext()) {
         Console.Write(enumerator.Current + " ");
      }
      Console.WriteLine();
      
      // Reset and iterate again
      enumerator.Reset();
      Console.WriteLine("Second iteration:");
      while (enumerator.MoveNext()) {
         Console.Write(enumerator.Current + " ");
      }
      Console.WriteLine();
      
      enumerator.Dispose();
   }
}

The output of the above code is −

First iteration:
H e l l o 
Second iteration:
H e l l o 

Checking Character Positions

Here's an example that demonstrates the position tracking with MoveNext() −

using System;

public class Demo {
   public static void Main() {
      string word = "C#";
      CharEnumerator enumerator = word.GetEnumerator();
      
      int position = -1; // starts before first character
      Console.WriteLine("Initial position: " + position);
      
      while (enumerator.MoveNext()) {
         position++;
         Console.WriteLine("Position " + position + ": " + enumerator.Current);
      }
      
      Console.WriteLine("MoveNext() returns: " + enumerator.MoveNext());
      enumerator.Dispose();
   }
}

The output of the above code is −

Initial position: -1
Position 0: C
Position 1: #
MoveNext() returns: False

Conclusion

The CharEnumerator.MoveNext() method advances the enumerator to the next character and returns true if successful or false if it reaches the end. It is commonly used in while loops to iterate through each character of a string, providing fine-grained control over string traversal.

Updated on: 2026-03-17T07:04:35+05:30

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements