Alex Rivera | Logout

Generating an array of letters in the alphabet

Asked 2008-11-24T15:25:46.113
116

Is there an easy way to generate an array containing the letters of the alphabet in C#? It's not too hard to do it by hand, but I was wondering if there was a built in way to do this.

Edit
Report

2 Answers

119

C# 3.0 :

char[] az = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToArray();
foreach (var c in az)
{
    Console.WriteLine(c);
}

yes it does work even if the only overload of Enumerable.Range accepts int parameters ;-)

answered 2008-11-24T15:39:08.160
14

Assuming you mean the letters of the English alphabet...

    for ( int i = 0; i < 26; i++ )
    {
        Console.WriteLine( Convert.ToChar( i + 65 ) );
    }
    Console.WriteLine( "Press any key to continue." );
    Console.ReadKey();
answered 2008-11-24T15:36:08.120

Your Answer