November 30, 2009

Alphabet in C#

I saw some code that someone wrote at our company and he was reading the English alphabet from a config file. Granted, that I don't like that...I was looking for an easy way to do it and add to an array not from a config...or build it on the fly. I know I can easily add it to an array like:
char[] alpheng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
I came across this:
char[] az = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToArray();
foreach (var c in az)
{
Console.WriteLine(c);
}
Even funnier was that the suggested answer was what I said at the begining of this post and then someone came after that and gave this unique solution.

No comments:

Post a Comment