[ACCEPTED]-C# codes to get a list of strings like A to Z?-visual-studio-2005
from ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" select ch + ":";
0
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 + ":");
}
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) + ":");
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() + ':'));
How about:
var list = Enumerable.Range('a', 'z' - 'a' + 1).Select(charCode => (char)charCode)).ToList();
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).
Other answer ;-)
List<string> list = new List<string>();
for (int i = 'A'; i <= 'Z'; i++)
{
list.Add(string.Format("{0}:", Convert.ToChar(i)));
}
0
For testing code snippets, I use LinqPad or Snippet Compiler. I 1 prefer LinqPad but both are very nice.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.