Charenumerator.Reset() Method In C#

In this guide, you will discover the functionality of the CharEnumerator.Reset method in C#, including its objectives, syntax, sample illustrations, and practical uses.

What is the CharEnumerator.Reset function?

The CharEnumerator.Reset method belongs to the CharEnumerator class within the System namespace. When invoked, this function resets the CharEnumerator object back to its initial state, allowing you to resume traversing a string or a portion of a string from the beginning.

CharEnumerator's purpose:

Before delving into the details of Reset, it's crucial to grasp the role of CharEnumerator. This particular class enables the iteration through elements within a string in a more managed and effective way compared to the conventional for-each approach.

The CharEnumerator class implements the IEnumerator interface and is commonly employed for traversing characters within a string. Accessing it is achieved by invoking the string's GetEnumerator method. When you obtain a CharEnumerator instance, you are able to navigate through the characters within the associated string.

Syntax:

It has the following syntax:

Example

public void Reset ();

Example:

Filename: CharEnum.cs

Example

//Program to implement the  CharEnumerator.Reset() 
// Method in C#
using System; 

class Enumerator{ 

	// Driver code 
	public static void Main() 
	{ 
		// The string object initialization
		string str = "The Earth revolves around the sun."; 

		// the object of the class CharEnumerator 
		CharEnumerator chEn = str.GetEnumerator(); 

		// the result string
		while (chEn.MoveNext()) 
			Console.Write(chEn.Current); 

		// the object reset
		chEn.Reset(); 
		Console.WriteLine(); 

		// the string display at 2nd time
		while (chEn.MoveNext()) 
			Console.Write(chEn.Current); 
	} 
}

Output:

Output

The Earth revolves around the sun.
The Earth revolves around the sun.

Example 2:

FileName: CharEnum.cs

Example

//Program to implement the  CharEnumerator.Reset() 
// Method in C#
using System; 

class Enumerator{ 

	// Driver code 
	public static void Main() 
	{ 
		// The string object initialization
		string str = "0 2 4 6 8 9 10 12 14 16 18 20"; 

		// the object of the class CharEnumerator 
		CharEnumerator chEn = str.GetEnumerator(); 

		// the result string
		while (chEn.MoveNext()) 
			Console.Write(chEn.Current); 

		// the object reset
		chEn.Reset(); 
		Console.WriteLine(); 

		// the string display at 2nd time
		while (chEn.MoveNext()) 
			Console.Write(chEn.Current); 
	} 
}

Output:

Output

0 2 4 6 8 9 10 12 14 16 18 20
0 2 4 6 8 9 10 12 14 16 18 20

Advantages of using CharEnumerator.Reset Method in C#

There are several advantages of the CharEnumerator.Reset Method in C#. Some main advantages of the CharEnumerator.Reset Method are as follows:

  • Reusability: With the Reset function, you may iterate over the characters in a string numerous times using the same CharEnumerator object. When you reach the end of the string, you may reset the enumerator to the beginning to save resources and prevent having to generate a new enumerator.
  • Convenient for Custom Iteration Logic: If you have particular logic for iterating over the characters in the string and are required to restart it at a certain point. In that case, you can use the Reset method instead of establishing a new enumerator, which makes your code clearer and easier to read.
  • Performance: Reusing the same CharEnumerator instances using Reset may be more efficient than determining a new enumerator for each loop.
  • Efficient String Parsing: If you're manually parsing a string and require a restart from a specific place, the Reset function is very useful. It is frequently observed in cases involving customized DSL (Domain-Specific Language) processing or basic expression evaluation.

Input Required

This code uses input(). Please provide values below: