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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Reference:



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.