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

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
    //请按任意键继续. . .
}

相关文章:

  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案