Open In App

C# | CharEnumerator.Reset() Method

CharEnumerator.Reset Method is used to initializes the index to a position logically before the first character of the enumerated string.

Syntax:



public void Reset ();

Below are the programs to illustrate the use of CharEnumerator.Reset() Method:

Example 1:




// C# program to illustrate the
// use of CharEnumerator.Reset()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "The Sun rises in the East and sets in the West.";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        // Printing the string
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
  
        // Reset the CharEnumerator object
        chEnum.Reset();
        Console.WriteLine();
  
        // Printing the string again
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
    }
}

Output:

The Sun rises in the East and sets in the West.
The Sun rises in the East and sets in the West.

Example 2:




// C# program to illustrate the
// use of CharEnumerator.Reset()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "0 1 2 3 4 5 6 7 8 9";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        // Printing the string
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
  
        // Reset the CharEnumerator object
        chEnum.Reset();
        Console.WriteLine();
  
        // Printing the string again
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
    }
}

Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

Reference:


Article Tags :
C#