Reputation: 6238
I have searched this online, but I can't find the answer I am looking for.
Basically I have the 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 the following Dictionary?
Dictionary<int,string> myDic = new Dictionary<int,string>();
And myDic would look like this:
1, itemA
2, itemB
3, itemC
Any ideas?
Upvotes: 85
Views: 112095
Reputation: 1155
Another extension method that builds on Arithmomaniac's example:
/// <summary>
/// Returns a Dictionary<int, string> of the parent enumeration. Note that the extension method must
/// be called with one of the enumeration values, it does not matter which one is used.
/// Sample call: var myDictionary = StringComparison.Ordinal.ToDictionary().
/// </summary>
/// <param name="enumValue">An enumeration value (e.g. StringComparison.Ordinal).</param>
/// <returns>Dictionary with Key = enumeration numbers and Value = associated text.</returns>
public static Dictionary<int, string> ToDictionary(this Enum enumValue)
{
var enumType = enumValue.GetType();
return Enum.GetValues(enumType)
.Cast<Enum>()
.ToDictionary(t => (int)(object)t, t => t.ToString());
}
Upvotes: 7
Reputation: 113472
Try:
var dict = Enum.GetValues(typeof(fooEnumType))
.Cast<fooEnumType>()
.ToDictionary(t => (int)t, t => t.ToString() );
Upvotes: 213
Reputation: 4151
Here's the VB.NET version of Ani's answer:
Public Enum typFoo
itemA = 1
itemB = 2
itemC = 3
End Enum
Sub example()
Dim dict As Dictionary(Of Integer, String) = System.Enum.GetValues(GetType(typFoo)) _
.Cast(Of typFoo)() _
.ToDictionary(Function(t) Integer.Parse(t), Function(t) t.ToString())
For Each i As KeyValuePair(Of Integer, String) In dict
MsgBox(String.Format("Key: {0}, Value: {1}", i.Key, i.Value))
Next
End Sub
In my case, I wanted to save the path of important directories and store them in my web.config
file's AppSettings section. Then I created an enum to represent the keys for these AppSettings...but my front-end engineer needed access to these locations in our external JavaScript files. So, I created the following code-block and placed it in our primary master page. Now, each new Enum item will auto-create a corresponding JavaScript variable. Here's my code block:
<script type="text/javascript">
var rootDirectory = '<%= ResolveUrl("~/")%>';
// This next part will loop through the public enumeration of App_Directory and create a corresponding JavaScript variable that contains the directory URL from the web.config.
<% Dim App_Directories As Dictionary(Of String, App_Directory) = System.Enum.GetValues(GetType(App_Directory)) _
.Cast(Of App_Directory)() _
.ToDictionary(Of String)(Function(dir) dir.ToString)%>
<% For Each i As KeyValuePair(Of String, App_Directory) In App_Directories%>
<% Response.Write(String.Format("var {0} = '{1}';", i.Key, ResolveUrl(ConfigurationManager.AppSettings(i.Value))))%>
<% next i %>
</script>
NOTE: In this example, I used the name of the enum as the key (not the int value).
Upvotes: 3
Reputation: 22476
ArgumentException
if the type is not System.Enum
, thanks to Enum.GetValues
enum
constraint is available yet)public static Dictionary<T, string> ToDictionary<T>() where T : struct
=> Enum.GetValues(typeof(T)).Cast<T>().ToDictionary(e => e, e => e.ToString());
Upvotes: 7
Reputation: 6508
Use:
public static class EnumHelper
{
public static IDictionary<int, string> ConvertToDictionary<T>() where T : struct
{
var dictionary = new Dictionary<int, string>();
var values = Enum.GetValues(typeof(T));
foreach (var value in values)
{
int key = (int) value;
dictionary.Add(key, value.ToString());
}
return dictionary;
}
}
Usage:
public enum typFoo : int
{
itemA = 1,
itemB = 2,
itemC = 3
}
var mydic = EnumHelper.ConvertToDictionary<typFoo>();
Upvotes: 2
Reputation: 1541
See: How do I enumerate an enum in C#?
foreach( typFoo foo in Enum.GetValues(typeof(typFoo)) )
{
mydic.Add((int)foo, foo.ToString());
}
Upvotes: 61
Reputation: 4804
Adapting Ani's answer so that it can be used as a generic method (thanks, toddmo):
public static Dictionary<int, string> EnumDictionary<T>()
{
if (!typeof(T).IsEnum)
throw new ArgumentException("Type must be an enum");
return Enum.GetValues(typeof(T))
.Cast<T>()
.ToDictionary(t => (int)(object)t, t => t.ToString());
}
Upvotes: 21
Reputation: 11363
Using reflection:
Dictionary<int,string> mydic = new Dictionary<int,string>();
foreach (FieldInfo fi in typeof(typFoo).GetFields(BindingFlags.Public | BindingFlags.Static))
{
mydic.Add(fi.GetRawConstantValue(), fi.Name);
}
Upvotes: 1
Reputation: 30875
If you need only the name you don't have to create that dictionary at all.
This will convert enum to int:
int pos = (int)typFoo.itemA;
This will convert int to enum:
typFoo foo = (typFoo) 1;
And this will retrun you the name of it:
((typFoo) i).toString();
Upvotes: 0
Reputation: 41266
You can enumerate over the enum descriptors:
Dictionary<int, string> enumDictionary = new Dictionary<int, string>();
foreach(var name in Enum.GetNames(typeof(typFoo))
{
enumDictionary.Add((int)((typFoo)Enum.Parse(typeof(typFoo)), name), name);
}
That should put the value of each item and the name into your dictionary.
Upvotes: 3