namespace EnumExample { class Program { public enum TimeOfDay { Morning = 0, Afternoon = 1, Evening = 2 } static int Main(string[] args) { WriteGreeting(TimeOfDay.Morning); return 0; } static void WriteGreeting(TimeOfDay timeOfDay) { // the output through switch case is "Good morning!" 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; } // the output for following two lines is "Afternoon" TimeOfDay time = TimeOfDay.Afternoon; Console.WriteLine(time.ToString()); // ToString() method is used // the output of following two lines also "Afternoon" TimeOfDay time3 = TimeOfDay.Afternoon; Console.WriteLine(time3); // the same output is coming even the ToString() method was not used //============================================================= // the output for following two lines is "2" TimeOfDay time2 = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "evening", true); // Enum.Parse method is used Console.WriteLine((int)time2); // the output of following two lines also "2" TimeOfDay time4 = TimeOfDay.Evening; // the same output is coming even the Enum.Parse method was not used Console.WriteLine((int)time4); } } }
ToString()
.ToString()
Enum.Parse()
TimeOfDay.Evening
int
Int32
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)