📅  最后修改于: 2023-12-03 14:40:29.018000             🧑  作者: Mango
CharEnumerator.ToString()
method in C# returns a string that represents the current object. The string contains all the characters in the enumeration of the current CharEnumerator object.
public override string ToString ();
This method returns a string that contains all the characters in the enumeration of the current CharEnumerator object.
string str = "Hello World!";
CharEnumerator charEnum = str.GetEnumerator();
while (charEnum.MoveNext())
{
Console.Write(charEnum.Current.ToString());
}
Output:
Hello World!
In the above example, we can see that we have created a string "Hello World!" and then created an object of CharEnumerator class by using str.GetEnumerator()
. We have then looped through each character in the string using the MoveNext()
method of CharEnumerator object and printed it to the console using Console.Write()
method.
We can also use ToString()
method of CharEnumerator object to get the string representation of the characters in the enumeration, as shown below:
string str = "Hello World!";
CharEnumerator charEnum = str.GetEnumerator();
string charString = charEnum.ToString();
Console.WriteLine(charString);
Output:
Hello World!
In the above example, we can see that we have created a string "Hello World!" and then created an object of CharEnumerator class by using str.GetEnumerator()
. We have then used ToString()
method of CharEnumerator object to get the string representation of the characters in the enumeration and printed it to the console using Console.WriteLine()
method.
CharEnumerator.ToString() method in C# is a useful method to get the string representation of the characters in the enumeration of the CharEnumerator object. It can be used in situations where we want to get a string representation of the characters instead of looping through each character in the enumeration.