One of the methods:
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 create an infinite sequence (limited by the maximum long value) with slightly more complex 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 happens as follows: A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... etc .:
foreach (var s in generate().Take(200)) Console.WriteLine(s);
Vlad
source share