s
o
u
l
s
j
i
e

C#Enum枚举申明、使用、转换

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();
        }
    }
}

  

posted @ 2021-06-30 11:36  soulsjie  阅读(104)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43