[ACCEPTED]-C# codes to get a list of strings like A to Z?-visual-studio-2005

Accepted answer
Score: 14
from ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" select ch + ":";

0

Score: 12

Using LINQ:

List<string> aToZ = Enumerable.Range('A', 26)
                              .Select(x => (char) x + ":")
                              .ToList();

Not using LINQ - a simpler alternative 1 (IMO) to the original for loop:

List<string> list = new List<string>();
for (char c = 'A'; c <= 'Z'; c++)
{
   list.Add(c + ":");
}
Score: 4

Well, not counting missing ')' at the end 3 of list.Ad.... line, everything is ok, altough 2 you could write it using a bit shorter notation 1

list.Add((char)('A' + i) + ":");
Score: 2

Edit: Y'all should have marked me down for 2 replying without reading. This doesn't work 1 in VS2005, which is what the OP asked about.

List<string> list = new List<string>(Enumerable.Range((int)'A', 26).Select(value => ((char)value).ToString() + ':'));
Score: 1

How about:

var list = Enumerable.Range('a', 'z' - 'a' + 1).Select(charCode => (char)charCode)).ToList();

0

Score: 0

Yours works fine with the exception of missing 2 a ). I test all my snipits with LinqPad. (I don't 1 know how I ever lived without it).

Score: 0

Other answer ;-)

List<string> list = new List<string>();
for (int i = 'A'; i <= 'Z'; i++)
{
    list.Add(string.Format("{0}:", Convert.ToChar(i)));
}

0

Score: 0

For testing code snippets, I use LinqPad or Snippet Compiler. I 1 prefer LinqPad but both are very nice.

More Related questions