xiaowei-blog

导航

Enum枚举

1.定义

enum  orientation :byte
    {
         north=1,
         south=2,
         east=3,
         west=4
    }

2.使用

 //定义enum      

       orientation myOrientation = orientation.east;    

         System.Console.WriteLine(myOrientation);

            //enum->byte        

     byte directionByte = (byte)myOrientation;      

       System.Console.WriteLine(directionByte);

            //byte->enum ; 如果byte值未映射到enum中的一个值,会产生逻辑错误,但不会抛出异常;  

           myOrientation = (orientation)directionByte;         

          System.Console.WriteLine(myOrientation);

               //enum->toString()        

                 string directionStr = Convert.ToString(myOrientation);

            //String->enum; 如果值不能映射为enum中的一个值,会抛异常;    

            myOrientation = (orientation)Enum.Parse(typeof(orientation), "north");      

         System.Console.WriteLine(myOrientation);

posted on 2014-12-25 16:50  xiaowei-blog  阅读(117)  评论(0编辑  收藏  举报

欢 迎 大 家光 临 我 的 个 人 博 客 !