用C#枚举到字典 [英] Enum to Dictionary in C#

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

问题描述

我在网上搜索了此内容,但找不到所需的答案.

I have searched this online, but I can't find the answer I am looking for.

基本上我有以下枚举:

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

如何将该枚举转换为Dictionary,以便将其存储在以下Dictionary中?

How can I convert this enum to Dictionary so that it stores in the following Dictionary?

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

myDic看起来像这样:

And myDic would look like this:

1, itemA
2, itemB
3, itemC

有什么想法吗?

推荐答案

请参阅:

See: How do I enumerate an enum in C#?

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

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

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