在C#中,我怎样才能建立阵列,从A到ZZ是类似出类拔萃的订单列的方式

问题描述:

我要寻找code,可以生成一个数组,其中第一项是 A B C 。 。 。之后的以Z 那就再转到 AA AB AC 。 。 。一路攀升为 ZZ

i am looking for code that can generate an array where the first item is A, then B, then C . . .after Z it would then go to AA, then AB then AC . . . all the way up to ZZ.

什么是C#这样做的最好的方法是什么?

what is the best way of doing this in C#?

方法之一是:

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 });
}


编辑:
你实际上可以产生无限序列(由最大数值界)有较为复杂的code:



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);
}

这一次是这样的:A,B,...,Z,AA,AB,...,ZZ,AAA,AAB,...等等:

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

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