枚举是用户定义的整数类型。
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;}}} }