R J

R J

  • NA
  • 380
  • 70.5k

Help on Enumerations

Sep 27 2010 1:57 AM
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);



        }

    }
}



 now my doubt is...
1) without using ToString() method also i'm getting same output  "Afternoon".
2) without using Enum.Parse() method also i'm getiing same output  "2".

then what are the cases that i can use this two methods???

please suggest me in this topic.....

thank you


Answers (1)