从int枚举继承int

2023-09-03 03:13:46 作者:孤凉。

我发现这一切在这个code的地方:

I've found this all over the place in this code:

public enum Blah: int
{
    blah = 0,
    blahblah = 1
}

为什么会需要从INT继承?是否任何时候需要?

Why would it need to inherit from int? Does it ever need to?

推荐答案

按照文档:

每个枚举类型都有一个   基础类型,可以是任何   整型除了字符。默认   枚举的基础类型   元素数据类型为int。

Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int.

所以,不,你不需要使用int。它将与任何整型。如果不指定任何它将使用int作为默认,它的这种类型的将被用于存储枚举到存储器

So, no, you don't need to use int. It would work with any integral type. If you don't specify any it would use int as default and it's this type that will be used to store the enumeration into memory.