枚举字典C# [英] Enum to Dictionary c#

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

问题描述

我上网查查看,但无法找到我要找的答案。
基本上我有以下枚举:

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>();

和mydic会是这样的:

and mydic would looks like this:

1, itemA
2, itemB
3, itemC

任何想法?

推荐答案

看:如何我列举一个枚举?

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

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

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