CharEnumerator.ToString() Method in C#

The CharEnumerator.ToString() method in C# returns a string representation of the current CharEnumerator object. This method is inherited from the Object class and provides basic type information about the enumerator instance.

Syntax

Following is the syntax −

public override string ToString();

Return Value

The method returns a string that represents the current object. For CharEnumerator, this typically returns the fully qualified type name "System.CharEnumerator".

Using CharEnumerator.ToString() Method

Example

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

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.WriteLine("String representation = " + ch.ToString());
        
        Console.WriteLine("\nEnumerating characters:");
        while (ch.MoveNext())
            Console.Write(ch.Current + " ");
        
        ch.Reset();
        Console.WriteLine("\nReset and enumerate again:");
        while (ch.MoveNext())
            Console.Write(ch.Current);
        Console.WriteLine();
    }
}

The output of the above code is −

HashCode = 31020903
Get the Type = System.CharEnumerator
String representation = System.CharEnumerator

Enumerating characters:
T h i s   i s   i t ! 
Reset and enumerate again:
This is it!

Comparison with Other Methods

Method Purpose Return Type
ToString() Returns string representation of the object string
GetType() Returns the exact runtime type Type
GetHashCode() Returns hash code for the object int

Practical Example

Example

Here's another example showing how ToString() can be useful for debugging or logging −

using System;

public class CharEnumeratorDemo {
    public static void Main() {
        string text = "Hello";
        CharEnumerator enumerator = text.GetEnumerator();
        
        Console.WriteLine("Enumerator info:");
        Console.WriteLine("Type: " + enumerator.ToString());
        Console.WriteLine("Equals check: " + enumerator.ToString().Equals("System.CharEnumerator"));
        
        // Demonstrate that ToString() doesn't change with enumeration state
        enumerator.MoveNext();
        Console.WriteLine("After MoveNext(): " + enumerator.ToString());
        
        enumerator.Reset();
        Console.WriteLine("After Reset(): " + enumerator.ToString());
    }
}

The output of the above code is −

Enumerator info:
Type: System.CharEnumerator
Equals check: True
After MoveNext(): System.CharEnumerator
After Reset(): System.CharEnumerator

Conclusion

The CharEnumerator.ToString() method returns the fully qualified type name "System.CharEnumerator". While not commonly used in everyday programming, it can be helpful for debugging, logging, or when you need to identify the type of an enumerator object at runtime.

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

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements