玩转C科技.NET

从学会做人开始认识这个世界!http://volnet.github.io

导航

【代码保留】枚举的键与值

随手写的,同事突然迷惑于枚举类型

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

namespace ConsoleApplication1
{
    class Program
    {
        public enum EnumSample
        {
            nomeaningvalue = -1,
            value1 = 1,
            value2 = 21
        }

        static void Main(string[] args)
        {
            string key = "value2";

            List<int> values = new List<int>();
            values.Add(1);
            values.Add(21);

            EnumSample sample = GetEnum(key, values);

            if (sample != EnumSample.nomeaningvalue)
            {
                Console.WriteLine(sample.GetHashCode());
            }
            else
            {
                Console.WriteLine("Not exists the key = " + key);
            }
        }

        static EnumSample GetEnum(string compare,List<int> values)
        {
            EnumSample result = EnumSample.nomeaningvalue;
            foreach(int value in values)
            {
                EnumSample sample = (EnumSample)value;
                if (sample.ToString() == compare)
                {
                    result = sample;
                    break;
                }
            }
            return result;
        }
    }

    //21
    //请按任意键继续. . .
}

posted on 2007-11-24 02:17  volnet(可以叫我大V)  阅读(220)  评论(0编辑  收藏  举报

使用Live Messenger联系我
关闭