从int枚举继承 [英] Enum inheriting from int

查看:171
本文介绍了从int枚举继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这一切在这个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.

这篇关于从int枚举继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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