Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class MinhHoaC3
{
enum NhietDoNuoc
{
DoDong = 0,
DoNguoi = 20,
DoAm = 40,
DoNong = 60,
DoSoi = 100,
}
static void Main()
{
System.Console.WriteLine(NhietDoNuoc.DoDong);
System.Console.WriteLine(NhietDoNuoc.DoNguoi);
System.Console.WriteLine(NhietDoNuoc.DoAm);
System.Console.WriteLine(NhietDoNuoc.DoNong);
System.Console.WriteLine(NhietDoNuoc.DoSoi);
Console.Read();
}
}



Out:

DoDong
DoNguoi
DoAm
DoNong
DoSoi


X| X| X| X|
Don`t out:

0
20
40
60
100

:confused::confused::confused:
Posted

in C# language the enum is a class (see documentation[^]), in your code
System.Console.WriteLine(NhietDoNuoc.DoDong);
System.Console.WriteLine(NhietDoNuoc.DoNguoi);
System.Console.WriteLine(NhietDoNuoc.DoAm);
System.Console.WriteLine(NhietDoNuoc.DoNong);
System.Console.WriteLine(NhietDoNuoc.DoSoi);

the method toString() (see [^]) is implicitely called (incidentally this is a very useful feature of C# enums).
See (documentation, again) [^] for proper usage of enums.

:)
 
Share this answer
 
v3
Nothing crazy here... the default behaviour of Console.WriteLine when it receives an object is to call that object's ToString.

An Enum's ToString returns the string representation of the enum field, which is exactly what you see.

Interestingly for you, an enum can be explicitly cast to an integer to get the value behind each field:

class MinhHoaC3
{
enum NhietDoNuoc
{
DoDong = 0,
DoNguoi = 20,
DoAm = 40,
DoNong = 60,
DoSoi = 100,
}
static void Main()
{
System.Console.WriteLine((int)NhietDoNuoc.DoDong);
System.Console.WriteLine((int)NhietDoNuoc.DoNguoi);
System.Console.WriteLine((int)NhietDoNuoc.DoAm);
System.Console.WriteLine((int)NhietDoNuoc.DoNong);
System.Console.WriteLine((int)NhietDoNuoc.DoSoi);
Console.Read();
}
}
 
Share this answer
 

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