in c#, how can i build up array from A to ZZ that is similar to the way that excel orders columns

One of the ways is:

IEnumerable<string> generate()
{
    for (char c = 'A'; c <= 'Z'; c++)
        yield return new string(c, 1);
    for (char c = 'A'; c <= 'Z'; c++)
        for (char d = 'A'; d <= 'Z'; d++)
            yield return new string(new[] { c, d });
}

Edit:
you can actually produce "infinite" sequence (bounded by maximal long value) with somewhat more complicated code:

string toBase26(long i)
{
    if (i == 0) return ""; i--;
    return toBase26(i / 26) + (char)('A' + i % 26);
}

IEnumerable<string> generate()
{
    long n = 0;
    while (true) yield return toBase26(++n);
}

This one goes like that: A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... etc:

foreach (var s in generate().Take(200)) Console.WriteLine(s);

Great answer of Vlad.

Here's another variation on that:

 static IEnumerable<string> generate() {
   for (char c = 'A'; c <= 'Z'; c++) {
     yield return c.ToString();
   }

   foreach (string s in generate()) {
     for (char c = 'A'; c <= 'Z'; c++) {
       yield return s + c;
     }
   }
 }

If you don't mind starting the sequence with an empty string you could write it as follows:

 static IEnumerable<string> generate() {
   yield return "";

   foreach (string s in generate()) {
     for (char c = 'A'; c <= 'Z'; c++) {
       yield return s + c;
     }
   }
 }

Tags:

C#

Algorithm