user193276 user193276 - 1 year ago 89
C# Question

Coverting Enums to Key,Value Pairs

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

public enum Translation

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 Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download