activebiz activebiz - 29 days ago 10
C# Question

Enum to Dictionary c#

I have search this online but cant find the answer I am looking for.
Basically I have following enum:

public enum typFoo : int
{
itemA : 1,
itemB : 2
itemC : 3
}


How can I convert this enum to Dictionary so that it stores in following dictionary

Dictionary<int,string> mydic = new Dictionary<int,string>();


and mydic would looks like this:

1, itemA
2, itemB
3, itemC


Any ideas?

Answer

see: How do I enumerate an enum?

foreach( typFoo foo in Enum.GetValues(typeof(typFoo)) )
{
    mydic.Add((int)foo, foo.ToString());
}