Convert Enum To Dictionary .Net

Convert Enum To Dictionary .Net

Added by Sixth Sense updated on Thursday, December 7, 2017

Problem:

The following code snippet demonstrates how to convert Enum to Dictionary.

public Dictionary DictionaryFromEnum(Type enumType)
{
	if (!enumType.IsEnum)
    	throw new ArgumentException("Type must be an enum");
    Array values = Enum.GetValues(enumType);
    Dictionary result = new Dictionary(values.Length);
    foreach(var v in values)
    {
    	result.Add((int)v, v.ToString());
    }            
	return result;
} 				
				

Related Tags

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected