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

查看:51
本文介绍了在 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

有什么想法吗?

推荐答案

参见:如何在 C# 中枚举枚举?

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

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

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