Skip to content

Instantly share code, notes, and snippets.

@tdewilde
Created April 7, 2014 08:26
Show Gist options
  • Select an option

  • Save tdewilde/10016625 to your computer and use it in GitHub Desktop.

Select an option

Save tdewilde/10016625 to your computer and use it in GitHub Desktop.
Convert an Enum to a Dictionary<int, string>. Uses the extension method Enum.GetDescription() to retrieve the value.
public static Dictionary<int, string> GetEnumDictionary<T>() where T : struct
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T is not an Enum type");
return Enum.GetValues(typeof(T))
.Cast<object>()
.ToDictionary(k => (int)k, v => ((Enum)v).GetDescription());
}
@cw-patiltejashree
Copy link

cw-patiltejashree commented Feb 4, 2019

Good ! It is better if you try to make it either in int or short or byte means datatype independent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment