.net中枚举enum的复习与实例

internal enum BagType
        {
            Big,
            Middle,
            Small
        }

        static void Main(string[] args)
        {
            Console.WriteLine("------------ enum to other ---------------");

            BagType bagType = BagType.Small;
            Console.WriteLine("bagType:\t{0}",bagType);
            Console.WriteLine("bagType.ToString():\t{0}", bagType.ToString());

            int bagTypeValue = (int)bagType;
            Console.WriteLine("bagType To int :\t{0}", bagTypeValue);
            Console.WriteLine("bagType.Type:\t{0}", bagType.GetType());

            Console.WriteLine("------------------ other to enum ------------------");

            if (Enum.IsDefined(bagType.GetType(), bagTypeValue))
            {
                Console.WriteLine("Enum.IsDefined int :true");
            }
            else
            {
                Console.WriteLine("Enum.IsDefined int :false");
            }

            if (Enum.IsDefined(bagType.GetType(), "Small"))
            {
                Console.WriteLine("Enum.IsDefined string:true");
            }
            else
            {
                Console.WriteLine("Enum.IsDefined string:false");
            }

            bagType = (BagType)Enum.Parse(typeof(BagType), "Big");
            Console.WriteLine("bagType:\t{0}", bagType);

            bagType = (BagType)1;
            Console.WriteLine("bagType:\t{0}", bagType);

            BagType[] array = (BagType[])Enum.GetValues(typeof(BagType));
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("\t array[{0}]:{1}", i, array[i]);
            }


            Console.WriteLine(Enum.GetName(typeof(BagType),1));


            Console.WriteLine("---------------------- end -----------------------");
            Console.ReadKey();
        }
原文地址:https://www.cnblogs.com/shizioo/p/2555786.html