文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>重新把c#编程技术过一遍2(枚举)

重新把c#编程技术过一遍2(枚举)

时间:2010-12-13  来源:肉丝

下面是一个简单的例子:

 public enum TimeOfDay
    {
        Morning =0,
        Afternoon=1,
        Evening =2,
    }
    class EnumExamaple
    {
        public static int Main()
        {
            WriteGreeting(TimeOfDay.Afternoon);
            return 0;
        }
        static void WriteGreeting(TimeOfDay timeOfDay)
        {
            switch (timeOfDay)
            {
                case  TimeOfDay.Morning :
                    Console.WriteLine("good morning");
                    break ;
                case TimeOfDay.Afternoon:
                    Console.WriteLine("good afternoon");
                    break;
                case TimeOfDay.Evening:
                    Console.WriteLine("good evening");
                    break;
                default :
                    Console.WriteLine("hello");
                    break;

            }
        }
    }

 

*可以获取枚举字段的字符串表示

    TimeOfDay time = TimeOfDay.Afternoon;
            Console.WriteLine(time.ToString());

 

*将字符串变为枚举,然后获取枚举值

TimeOfDay time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon", true);
            Console.WriteLine((int)time);

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载