枚举到词典c# [英] Enum to Dictionary c#

查看:144
本文介绍了枚举到词典c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索,但找不到我正在寻找的答案。
基本上我有以下枚举:

  public enum typFoo:int 
{
itemA: 1,
itemB:2
itemC:3
}

如何将此枚举转换为字典,以便它存储在以下字典中

 字典< int,string> mydic = new Dictionary< int,string>(); 

,mydic将如下所示:

  1,itemA 
2,itemB
3,itemC

任何想法?

解决方案

请参阅:如何枚举枚举?

  foreach(在Typum(Typeof(typFoo))中的typupoo foo)
{
mydic.Add((int)foo,foo.ToString());
}


I have search this online but cant find the answer I am looking for. Basically I have 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 following dictionary

Dictionary<int,string> mydic = new Dictionary<int,string>();

and mydic would looks like this:

1, itemA
2, itemB
3, itemC

Any ideas?

解决方案

see: How do I enumerate an enum?

foreach( typFoo foo in Enum.GetValues(typeof(typFoo)) )
{
    mydic.Add((int)foo, foo.ToString());
}

这篇关于枚举到词典c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆