C# 枚举与switch用法
1 using System; 2 3 namespace Csharp 4 { 5 class Program 6 { 7 //枚举 8 public enum TimeOfDay 9 { 10 Morning=0, 11 Afternoon=1, 12 Evening=2 13 } 14 15 static void Main() 16 { 17 WriteGreeting(TimeOfDay.Morning); 18 WriteGreeting(TimeOfDay.Afternoon); 19 WriteGreeting(TimeOfDay.Evening); 20 Console.ReadLine(); 21 } 22 static void WriteGreeting(TimeOfDay timeofday) 23 { 24 //switch用法 25 switch (timeofday) 26 { 27 case TimeOfDay.Morning: 28 Console.WriteLine("Good morning!"); 29 break; 30 case TimeOfDay.Afternoon: 31 Console.WriteLine("Good afternoon!"); 32 break; 33 case TimeOfDay.Evening: 34 Console.WriteLine("Good evening!"); 35 break; 36 default: 37 Console.WriteLine("Hello!"); 38 break; 39 } 40 } 41 } 42 }