• 【C#-枚举】枚举的使用


    枚举是用户定义的整数类型。

    namespace ConsoleApplication1
    {
        /// <summary>
        /// 在枚举中使用一个整数值,来表示一天的阶段
        /// 如:TimeOfDay.Morning返回数字0
        /// </summary>
        class EnumExample
        {
            public enum TimeOfDay
            {
                Morning = 0,
                Afternoon = 1,
                Evening = 2
            }
    
            public static void Main()
            {
                WriteGreeting(TimeOfDay.Morning);
    
                //获取枚举的字符串表示
                TimeOfDay time = TimeOfDay.Afternoon;
                Console.WriteLine(time.ToString());  //返回字符串Afternoon
    
                //从字符串中获取枚举值,并转换为整数
                TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon", true);
                Console.WriteLine((int)time2); //返回数字1
    
                Console.ReadKey();
            }
    
            /// <summary>
            /// 把枚举合适的值传给方法,并在switch中迭代可能的值
            /// </summary>
            /// <param name="timeOfDay"></param>
            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;
                }
            }
        }
    }
  • 相关阅读:
    标题
    Ubuntu配置 PPTP 服务器端
    网络虚拟化问题小记
    DevStack部署Openstack环境
    Ubuntu LVM扩展LV
    Gnocchi+Aodh服务简析
    部署Ceilometer +Gnocchi + Aodh
    Runing MAC on KVM 问题小记
    处理 Ceilometer UPD 丢包
    TC limit bandwidth
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/5694508.html
Copyright © 2020-2023  润新知