using System;

namespace _02EnumDemo
{
    class Program
    {
        //不带Key的申明,key自动按0递增
        enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
        //指定Name和Key申明枚举
        enum BookType
        {
            type1 = 11,
            type2 = 22,
            type3 = 33

        };
        static void Main(string[] args)
        {
            //取枚举Name  /  枚举转字符串
            string name = Days.Sun.ToString();
            Console.WriteLine(name);
            //根据Key取Name
            string name2 = Enum.GetName(typeof(Days), 5).ToString();
            Console.WriteLine(name2);
            //根据Name取Key
            string key = ((Int32)BookType.type1).ToString();
            Console.WriteLine(key);
            //判断指定Key是否存在枚举中
            int findKey = 12;
            bool b = Enum.IsDefined(typeof(BookType), findKey);
            Console.WriteLine(findKey + ""+ (b ? "存在BookType中" : "不存在BookType中"));
            //字符串转枚举
            BookType dbType = (BookType)Enum.Parse(typeof(BookType), "type1", true);
            Console.ReadKey();
        }
    }
}

  

相关文章: