1

Is it possible to create a dictionary of enum values? Example below what I need.

Example

key = 0, value = "Unknown"
key = 1, value = "Another"
etc..

Code snippet of enum

public enum MyEnum
{
    Unknown = 0,
    Another = 1,
    ...
}

My attempt

 var values = Enum.GetValues(typeof(MyeNum));
 var namesames = Enum.GetNames(typeof(MyeNum));


 // Save values
 foreach (var i in values )
 {
     dictionaryList.Add(i, "");
 }
user247702
  • 23,641
  • 15
  • 110
  • 157
James Radford
  • 1,815
  • 4
  • 25
  • 40

2 Answers2

5
Dictionary<int, string> values = 
     Enum.GetValues(typeof(MyEnum))
         .Cast<MyEnum>()
         .ToDictionary(e => (int)e, e => e.ToString());
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0
 Dictionary<int, string> dict = new Dictionary<int, string>();

 foreach (var v in Enum.GetValues(typeof(MyEnum)))
     dict.Add((int)v, v.ToString());
w.b
  • 11,026
  • 5
  • 30
  • 49