C# 枚举类型与switch case

namespace C1
{
    class Program
    {
        public enum TimeofDay
        {
            Morning = 0,
            Afternoon = 1,
            Evening = 2,
        }

        public static int Main()
        {
            WriteGreeting(TimeofDay.Morning);
            Console.ReadLine();
            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;
            }
        }
    } 
}

使用枚举一般是把合适的值传送给方法,并在switch语句中迭代可能的值。

你可能感兴趣的:(C#)