Little more "complicated" (maybe overkill) but I use these two methods to return dictionaries to use as datasources. The first one returns the name as key and the second value as key.
public static IDictionary ConvertEnumToDictionaryNameFirst()
{
if (typeof(K).BaseType != typeof(Enum))
{
throw new InvalidCastException();
}
return Enum.GetValues(typeof(K)).Cast().ToDictionary(currentItem
=> Enum.GetName(typeof(K), currentItem));
}
Or you could do
public static IDictionary ConvertEnumToDictionaryValueFirst()
{
if (typeof(K).BaseType != typeof(Enum))
{
throw new InvalidCastException();
}
return Enum.GetNames(typeof(K)).Cast().ToDictionary(currentItem
=> (int)Enum.Parse(typeof(K), currentItem));
}
This assumes you are using 3.5 though. You'd have to replace the lambda expressions if not.
Use:
Dictionary list = ConvertEnumToDictionaryValueFirst();
using System;
using System.Collections.Generic;
using System.Linq;