user193276 user193276 - 1 month ago 20
C# Question

Converting Enums to Key,Value Pairs

How to convert Enum to Key,Value Pairs. I have converted it in C# 3.0 .

public enum Translation
{
English,
Russian,
French,
German
}

string[] trans = Enum.GetNames(typeof(Translation));

var v = trans.Select((value, key) =>
new { value, key }).ToDictionary(x => x.key + 1, x => x.value);


In C# 1.0 What is the way to do so?

Answer

In C# 1...

string[] names = Enum.GetNames(typeof(Translation));

Hashtable hashTable = new Hashtable();
for (int i = 0; i < names.Length; i++)
{
    hashTable[i + 1] = names[i];
}

Are you sure you really want a map from index to name though? If you're just using integer indexes, why not just use an array or an ArrayList?