C#枚举与switch用法

using System;

namespace Csharp
{
    class Program
    {
        //枚举
        public enum TimeOfDay
        {
            Morning=0,
            Afternoon=1,
            Evening=2
        }

        static void Main()
        {
            WriteGreeting(TimeOfDay.Morning);
            WriteGreeting(TimeOfDay.Afternoon);
            WriteGreeting(TimeOfDay.Evening);
            Console.ReadLine();
        }
        static void WriteGreeting(TimeOfDay timeofday)
        {
            //switch用法
            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;
            }
        }
    }
}

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