The C# GetEnumerator function is employed to transform a string object into a character enumerator, which yields an instance of CharEnumerator. This enables the iteration of a string using a loop.
Signature
Example
public CharEnumerator GetEnumerator()
Parameter
It does not take any argument.
Return
It returns System.CharEnumerator.
C# String GetEnumerator Method Example
Example
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s2 = "Hello C#";
CharEnumerator ch = s2.GetEnumerator();
while(ch.MoveNext()){
Console.WriteLine(ch.Current);
}
}
}
Output:
Output
H
e
l
l
o
C
#