So this question is prompted by two things.
- I found some code in our source control doing this sort of things.
- These SO questions:
- https://stackoverflow.com/questions/297213/translate-an-index-into-an-excel-column-name
- https://stackoverflow.com/questions/837155/fastest-function-to-generate-excel-column-letters-in-c-sharp
- https://stackoverflow.com/questions/4075656/how-to-get-continuous-characters-in-c/4077835#4077835
- https://stackoverflow.com/questions/1011732/iterating-through-the-alphabet-c-sharp-a-caz
So when I thought about this problem this popped into my head almost immediately.
class Util
{
private static string[] alphabetArray = { string.Empty, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
public static IEnumerable<string> alphaList = alphabetArray.Cast<string>();
public static string IntToAA(int value)
{
while (Util.alphaList.Count() -1 < value)
{
Util.IncreaseList();
}
return Util.alphaList.ElementAt(value);
}
private static void IncreaseList()
{
Util.alphaList = Util.alphabetArray.Take(1).Union(
Util.alphaList.SelectMany(currentLetter =>
Util.alphabetArray.Skip(1).Select(innerLetter => currentLetter + innerLetter)
)
);
}
}
My question is this: Is this approach a better solution (performance wise)? or is a recursive / computed value better (eg. this answer )?