c# 将枚举作为键值的形式存储

来源:互联网 发布:施工用电方案软件 编辑:程序博客网 时间:2024/04/30 19:14

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace 将枚举作为键值的形式存储
{
    enum Myenum
    {
       First=3,
       Second,
       Third
    }
    class Program
    {
        static void Main(string[] args)
        {
            //用hashtable可以实现吗? NameValueCollection可以实现吗?
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            string [] enumArray=Enum.GetNames(typeof(Myenum));
            for (int i = 0; i < enumArray.Length; i++)
            {
                dictionary.Add(enumArray[i],(int) Enum.Parse(typeof(Myenum), enumArray[i]));
            }
            Console.WriteLine(dictionary["First"]);
            Console.WriteLine(dictionary["Second"]);
            Console.WriteLine(dictionary["Third"]);
            Console.Read();

         
        }
    }
}

原创粉丝点击