是否可以创建枚举值字典? 我需要的例子如下。
示例
key = 0, value = "Unknown"
key = 1, value = "Another"
etc..
枚举的代码段
public enum MyEnum
{
Unknown = 0,
Another = 1,
...
}
我的尝试
var values = Enum.GetValues(typeof(MyeNum));
var namesames = Enum.GetNames(typeof(MyeNum));
// Save values
foreach (var i in values )
{
dictionaryList.Add(i, "");
}
答案 0 :(得分:5)
Dictionary<int, string> values =
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.ToDictionary(e => (int)e, e => e.ToString());
答案 1 :(得分:0)
Dictionary<int, string> dict = new Dictionary<int, string>();
foreach (var v in Enum.GetValues(typeof(MyEnum)))
dict.Add((int)v, v.ToString());