为什么枚举值需要显式转换 [英] Why do enum values req explicit conversions

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

问题描述

如果一个类继承自另一个类,比如Form继承自控件,那么

我可以将Form分配给Control类型的变量,而不需要

显式转换,例如


Form1 f = new Form1();

控制c = f;


枚举值继承自int但它没有被隐式转换:


Horizo​​ntalAlignment h = Horizo​​ntalAlignment.Center;

int i = h;


任何原因?


谢谢,

Michael

解决方案

区别在于Form是Control的子类。

继承的基本规则(那个人的名字又是什么?)是子类总是可以用
代替超类。


问题是枚举不是int的子类(或long或sbyte或

uint等)。在运行时,枚举被视为

底层枚举类型的盒装类型。这就是为什么你必须取消它才能获得整数

的价值。


HTH


Dale Preston

MCAD,MCDBA,MCSE

" Michael C" < MC ***** @ NOSPAMoptushome.com.au>在消息中写道

news:uO ************** @ TK2MSFTNGP14.phx.gbl ...

如果一个类继承自另一个类,比如Form继承自控件,
然后我可以将Form分配给Control类型的变量,而不需要进行显式转换,例如

Form1 f = new Form1( );
控制c = f;

枚举值继承自int,但它不会被隐式转换:

Horizo​​ntalAlignment h = Horizo​​ntalAlignment.Center;
int i = h;

任何原因?

谢谢,
Michael



枚举在运行时被装箱?这对我来说似乎不对。 (但是我

可能是错的。:)


我会说这更像是一个自我记录代码的问题。 />
一个人真的应该把枚举看作一个自包含的类型,即使它只是一个int(或其他),在它下面就是
。所以,如果你想要开始操纵它,就好像它是一个数字一样,你必须投出

来向读者举起一面旗帜:我在这里做一些不寻常的事情

!注意!


除此之外,我看不出任何理由。


来自C#规范:


在运行时,System.Enum类型的值可以为null或对任何枚举类型的盒装值的引用。


Dale


" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

枚举在运行时被装箱?这对我来说似乎不对。 (但我可能是错的。:)

我要说这更像是一个自我记录代码的问题。
一个人真的应该对待一个枚举作为一种自包含类型,即使它只是一个int(或其他)。所以,如果你想开始操纵它就好像它是一个数字,你必须施展
以便向读者举起一面旗帜:我正在做一些不寻常的事情
这里!注意!

除此之外,我看不出任何理由。



If a class inherits from another class, say Form inherits from control, then
I can assign the Form to a variable of type Control without needing an
explicit conversion, eg

Form1 f = new Form1();
Control c = f;

An enum value inherits from int but it doesn''t get implicitly converted:

HorizontalAlignment h = HorizontalAlignment.Center;
int i = h;

Any reason?

Thanks,
Michael

解决方案

The difference is that Form is a subclass of Control. A basic rule of
inheritance (what is that guy''s name again?) is that the subclass can always
be substituted for the superclass.

The problem is that an enum is not a subclass of an int (or long or sbyte or
uint, etc..). At run time, enums are treated as boxed types of the
underlying enum type. That is why you must unbox it to get the integer
value.

HTH

Dale Preston
MCAD, MCDBA, MCSE
"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:uO**************@TK2MSFTNGP14.phx.gbl...

If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an
explicit conversion, eg

Form1 f = new Form1();
Control c = f;

An enum value inherits from int but it doesn''t get implicitly converted:

HorizontalAlignment h = HorizontalAlignment.Center;
int i = h;

Any reason?

Thanks,
Michael



enums are boxed at run time? That doesn''t seem right to me. (But I
could be wrong. :)

I was going to say that it''s more a question of self-documenting code.
One really ought to treat an enumeration as a self-contained type, even
though it is, underneath it all, just an int (or whatever). So, if you
want to start manipulating it as if it were a number, you have to cast
in order to raise a flag to the reader: "I''m doing something unusual
here! Pay attention!"

Other than that I can''t see any reason.


From the C# specification:

At run-time, a value of type System.Enum can be null or a reference to a
boxed value of any enum type.

Dale

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

enums are boxed at run time? That doesn''t seem right to me. (But I
could be wrong. :)

I was going to say that it''s more a question of self-documenting code.
One really ought to treat an enumeration as a self-contained type, even
though it is, underneath it all, just an int (or whatever). So, if you
want to start manipulating it as if it were a number, you have to cast
in order to raise a flag to the reader: "I''m doing something unusual
here! Pay attention!"

Other than that I can''t see any reason.



这篇关于为什么枚举值需要显式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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