Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
       class Values
    {
        enum Temperatures
        {
            WickedCold=0,
            FreezingPoint=32,
            LightJacketWeather=60,
            SwimingWeather=72,
            BoilingPoint=212,
        }

        static void Main()
        {
            Console.WriteLine("Freezing Point of Water: {0}", (int)  Temperatures.FreezingPoint);
            Console.WriteLine("Boiling Point of Water: {0}", (int) Temperatures.BoilingPoint);
            Console.ReadLine();
        }
    }
}


I have recently started learning C# i am stucked in the concept of enumeration that why we are casting when displaying the output although enumeration by default is of type integer?
If i remove (int) the output is Freezing of water is: FreezingPoint? Please help
Posted

The default output for an enum uses the ToString method of an enum. You need to cast an emum to int to let the output formatter know that you want the int version of output.
 
Share this answer
 
Comments
th3 Lion 2-Jun-12 3:47am    
can you please explain what ToString method does?
BillW33 2-Jun-12 7:05am    
The ToString method is a part of every object and "It converts an object to its string representation so that it is suitable for display." The quote is from MSDN here: http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx. You could also get your answer much faster by going to Google and typing in "C# ToString" the MSDN entry is the first result.
th3 Lion 3-Jun-12 4:10am    
Thanx alot for the help. Really appreciate it
BillW33 3-Jun-12 7:01am    
Glad to help. :)

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