C#中的long值枚举 [英] Enum of long values in C#
本文介绍了C#中的long值枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么要声明?
public enum ECountry : long
{
None,
Canada,
UnitedStates
}
是否需要强制转换其任何值?
require a cast for any of its values?
long ID = ECountry.Canada;
// Error Cannot implicitly convert type 'ECountry' to 'long'.
// An explicit conversion exists (are you missing a cast?)
有没有办法获取
这也不起作用,例如:
public enum ECountry : long
{
None = 0L,
Canada = 1L,
UnitedStates=2L
}
推荐答案
问题不是不是,基础类型仍然是 int
。 long
,您可以为成员分配 long
个值。但是,您可以从不仅将枚举
值分配给整数类型而无需强制转换。这应该起作用:
The issue is not that the underlying type is still int
. It's long
, and you can assign long
values to the members. However, you can never just assign an enum
value to an integral type without a cast. This should work:
public enum ECountry : long
{
None,
Canada,
UnitedStates = (long)int.MaxValue + 1;
}
// val will be equal to the *long* value int.MaxValue + 1
long val = (long)ECountry.UnitedStates;
这篇关于C#中的long值枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文