首页 > 编程语言 > 详细

Enum枚举

时间:2014-12-25 18:01:03      阅读:302      评论:0      收藏:0      [点我收藏+]

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

Enum枚举

原文:http://www.cnblogs.com/xiaowei-blog/p/4185032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!