[英]Enum to int, why use type cast to int rather than Convert.ToInt32(Enum)?
Been looking at the Enum
to int
qa's, There are plenty of type casting of (int)Enum
, however I couldn't find any answers that suggests use of Convert.ToInt32(Enum)
. 一直在看
Enum
到int
qa,有很多类型转换(int)Enum
,但我找不到任何建议使用Convert.ToInt32(Enum)
答案。
Is there any reason for preferring typecast over using Convert.ToInt32 ? 有没有理由比使用Convert.ToInt32更喜欢使用类型转换?
By default, enums inherit from int
, ie behind the scenes it is: 默认情况下,枚举从
int
继承,即幕后它是:
enum Foo : int
{
A,
B
}
In C#, there is no difference between Int32
and int
. 在C#中,
Int32
和int
之间没有区别。 So using Convert.ToInt32(Foo.A)
is a bit redundant, since performing (int)Foo.A
will suffice. 所以使用
Convert.ToInt32(Foo.A)
有点多余,因为执行(int)Foo.A
就足够了。 Personally, (int)Foo.A
also reads much better. 就个人而言,
(int)Foo.A
也读得更好。 Note, you would only really have to watch this in situations where you defined your enum like so: 注意,你只需要在你定义你的枚举的情况下看这个:
enum Foo : long
{
A,
B
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.