在枚举中指定基础类型的用例是什么? [英] What's the use-case for specifying the underlying type in enums?
本文介绍了在枚举中指定基础类型的用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么要点
enum SomeEnum : byte // <----
{
SomeValue = 0x01,
...
}
你必须使用一个转换来将它分配给与枚举基础类型相同类型的变量?
when you have to make a cast just to assign it to the same type of variable as the enums underlying type?
byte b = (byte)SomeEnum.SomeValue;
推荐答案
底层类型( int
)是不够的,即。你想使用比那个更高的整数值,那么你可以使它 long
。当您有超过32个值的 [Flags]
枚举时,这可能很有用。
Not much point, really, except that if the default underlying type (int
) is not enough for you, ie. you want to use higher integer values than that then you can make it long
. This can be useful when you have a [Flags]
enum with more than 32 values.
您可以使它 byte
或 short
只是为了限制值的范围,但实际上仍然需要4个字节 int
)。
You can make it byte
or short
just to restrict the range of values, but it will actually still take 4 bytes (ie. same as int
).
这篇关于在枚举中指定基础类型的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文