Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all...

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
Posted

It's because, the ToString() method is implicitly being called in first cases, even if you don't call .ToString() explicitly.

And, the Enum.Parse() is required only if you need to convert a string value to an Enum representation. In your case, as you already have an Enum (TimeOfDay.Evening, which is internally assigned an int value), you will get the corresponding int value output.
 
Share this answer
 
v2
Comments
Hiren solanki 27-Sep-10 4:57am    
good answer Al-Farooque Shubho.
Ramesh Jallepalli 27-Sep-10 7:49am    
thank you
When you use a variable in a context where a string is required, the framework automatically calls its ToString() method, even if you don't do it explicitly.
In your code, if you cast the enumerated value to an int, then the framework calls the ToString() method of the Int32 class; this is why in some cases you get 2 as output.
 
Share this answer
 
Comments
Ramesh Jallepalli 27-Sep-10 7:49am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900