枚举疯狂??? [英] enum crazy ???

查看:75
本文介绍了枚举疯狂???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
}
}




出:

DoDong
DoNguoi
DoAm
同农
DoSoi


X | X | X | X |
不要出门:

0
20
40
60
100

:confused :: confused :: confused:




Out:

DoDong
DoNguoi
DoAm
DoNong
DoSoi


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

0
20
40
60
100

:confused::confused::confused:

推荐答案

C#语言表示,enum是一个类(请参见文档[
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);


方法toString()(请参阅[
^ ])被隐式调用(顺便说一下,这是C# enum s的一个非常有用的功能).
请参见(再次提供文档)[ ^ ]正确使用enum s.

:)


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.

:)


这里没什么疯狂... Console.WriteLine收到对象时的默认行为是调用该对象的ToString.

一个Enum的ToString返回枚举字段的字符串表示形式,这正是您所看到的.

对您而言有趣的是,可以将枚举显式转换为整数以获取每个字段后面的值:

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();
}
}


这篇关于枚举疯狂???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆