C# String GetEnumerator Method Example

In this source code example, we will demonstrate the usage of String.GetEnumerator() method in C# with an example.

The C# String.GetEnumerator() method is used to convert a string object into a char enumerator. It returns an instance of CharEnumerator. Once you got CharEnumerator then you can iterate the string through the loop.

C# String GetEnumerator Method Example

The following example demonstrates the String.GetEnumerator() method:
using System;

public class StringDemo
{
    public static void Main(string[] args)
    {
        string s1 = "sourcecodeexamples";
        
        CharEnumerator ch = s1.GetEnumerator();  
        while(ch.MoveNext()){  
            Console.WriteLine(ch.Current);  
        }   

    }

}

Output:

s
o
u
r
c
e
c
o
d
e
e
x
a
m
p
l
e
s

Related C# String Examples

References



Comments