CharEnumerator.Reset() Method in C#

The CharEnumerator.Reset() method in C# initializes the enumerator's position to logically before the first character of the enumerated string. This method is useful when you need to restart enumeration from the beginning after having already iterated through the string.

Syntax

Following is the syntax for the CharEnumerator.Reset() method −

public void Reset();

Parameters

This method takes no parameters.

Return Value

This method does not return any value (void).

How It Works

When you call Reset(), the enumerator's internal position is moved to before the first character. After calling Reset(), you must call MoveNext() to advance to the first character before accessing Current.

CharEnumerator Position Flow Initial Position After MoveNext() Enumeration After Reset() Before first char At first char Continue iterating Back to start

Example

The following example demonstrates how to use the Reset() method to enumerate a string twice −

using System;

public class Demo {
   public static void Main() {
      string strNum = "This is it!";
      CharEnumerator ch = strNum.GetEnumerator();
      
      Console.WriteLine("HashCode = " + ch.GetHashCode());
      Console.WriteLine("Get the Type = " + ch.GetType());
      
      Console.Write("First enumeration: ");
      while (ch.MoveNext())
         Console.Write(ch.Current + " ");
      
      ch.Reset();
      Console.WriteLine();
      Console.Write("After Reset(): ");
      while (ch.MoveNext())
         Console.Write(ch.Current);
      
      Console.WriteLine();
   }
}

The output of the above code is −

HashCode = 65311716
Get the Type = System.CharEnumerator
First enumeration: T h i s   i s   i t ! 
After Reset(): This is it!

Using Reset() with Specific Character Search

Example

using System;

public class CharSearchDemo {
   public static void Main() {
      string text = "Hello World";
      CharEnumerator enumerator = text.GetEnumerator();
      
      // First pass: count vowels
      int vowelCount = 0;
      while (enumerator.MoveNext()) {
         char c = char.ToLower(enumerator.Current);
         if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            vowelCount++;
         }
      }
      Console.WriteLine("Vowels found: " + vowelCount);
      
      // Reset and second pass: count consonants
      enumerator.Reset();
      int consonantCount = 0;
      while (enumerator.MoveNext()) {
         char c = char.ToLower(enumerator.Current);
         if (char.IsLetter(c) && !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
            consonantCount++;
         }
      }
      Console.WriteLine("Consonants found: " + consonantCount);
   }
}

The output of the above code is −

Vowels found: 3
Consonants found: 7

Common Use Cases

  • Multiple iterations over the same string without creating a new enumerator.

  • Character analysis requiring multiple passes through the string data.

  • Resetting enumeration state after conditional logic or error handling.

Conclusion

The CharEnumerator.Reset() method provides an efficient way to restart string enumeration from the beginning. It's particularly useful when you need to perform multiple passes over the same string data without creating new enumerator instances.

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

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements