枚举继承自 int

发布于 2024-11-15 19:28:08 字数 139 浏览 3 评论 0原文

我在这段代码中到处都发现了这一点:

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

为什么它需要从 int 继承?有必要这样做吗?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

游魂 2024-11-22 19:28:08

根据文档

每个枚举类型都有一个
基础类型,可以是任何
除 char 之外的整型。默认
枚举的基础类型
元素是整数。

所以,不,你不需要使用 int。它适用于任何整数类型。如果您没有指定任何类型,它将使用 int 作为默认值,并且这种类型将用于将枚举存储到内存中。

According to the documentation:

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.

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.

明月夜 2024-11-22 19:28:08

枚举由整数隐式支持。
: int 只是重申默认值,就像 void M();private void M(); 一样。

您还可以创建由其他整数类型支持的枚举,例如 enum GiantEnum : long

Enums are implicitly backed by integers.
: int just restates the default, just like void M(); vs. private void M();.

You can also create enums that are backed by other intergral types, such as enum GiantEnum : long.

小忆控 2024-11-22 19:28:08

不需要,这是暗示的。 根据 MSDN

枚举是一组命名常量,其基础类型是除 Char 之外的任何整型。如果没有显式声明基础类型,则使用 Int32。 Enum 是 .NET Framework 中所有枚举的基类。

这意味着您可以使用bytesbyteushortintuintlongulong

另外,按照您所描述的方式设置值(blah = 0,blahblah = 1)虽然是多余的,但也可以,因为根据 C# 规范

如果枚举成员的声明没有初始值设定项,则隐式设置其关联值,如下所示:

• 如果枚举成员是枚举类型中声明的第一个枚举成员,则其关联值为零。

• 否则,通过将文本上前面的枚举成员的关联值加一来获得枚举成员的关联值。这个增加的值必须在底层类型可以表示的值范围内,否则会出现编译时错误。

You don't need to, it's implied. According to MSDN:

An enumeration is a set of named constants whose underlying type is any integral type except Char. If no underlying type is explicitly declared, Int32 is used. Enum is the base class for all enumerations in the .NET Framework.

This means you could usebyte, sbyte, ushort, int, uint, long, or ulong.

Also, setting the values the way you have described (blah=0, blahblah=1), while redundant, is OK, since, according to the C# Specification

If the declaration of the enum member has no initializer, its associated value is set implicitly, as follows:

• If the enum member is the first enum member declared in the enum type, its associated value is zero.

• Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one. This increased value must be within the range of values that can be represented by the underlying type, otherwise a compile-time error occurs.

夏日落 2024-11-22 19:28:08

int 默认情况下是任何enum 的类型。它不需要显式声明。

当您想使用其他内容(bytelong 等)时,它会更有用。

int is by default the type of any enum. It does not need to be declared explicitly.

It's more useful when you want to use something else (byte, long, and friends).

明媚如初 2024-11-22 19:28:08

您不需要需要int继承,但默认情况下是这样。如果您愿意,您可以从其他整数类型(bytelong 等)继承。一个例子是,如果您想节省数据库中的内存或列空间。

You don't need to inherit from int but by default it does. You can inherit from other integral types (byte, long, etc) if you want to. An example would be if you wanted to save memory or column space in a DB.

合约呢 2024-11-22 19:28:08

大多数时候,我不关心枚举是否有符号,或者它有多少位,所以我只是让系统使用它的默认值 int 。

然而,有时我确实关心枚举是有符号的 32 位 int,那么最好明确表示我确实关心。我希望有评论能够阐明为什么我关心。

Most of the time I don’t care if an enum is signed or unsigned, or how many bits it has, so I just let the system use it’s default that is int.

However there are times when I do care that the enum is a signed 32 bit int, and then it is good to make it clear that I do care. I would expect a comment as well spelling out why I care.

清君侧 2024-11-22 19:28:08

据我所知,它给了它一个数值,仅此而已

it gives it a numeric value, that is all, as far as i know

走过海棠暮 2024-11-22 19:28:08

您不需要继承,因为默认情况下 Enum 的基本类型是 int。

http://msdn.microsoft.com/en-我们/library/sbbt4032(v=vs.71).aspx

base-type (Optional)
The underlying type that specifies the storage allocated for each enumerator. It can be one of the integral types except char. The default is int.

You do not need to inherit as the base type of an Enum is by default, int.

http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.71).aspx

base-type (Optional)
The underlying type that specifies the storage allocated for each enumerator. It can be one of the integral types except char. The default is int.
鱼窥荷 2024-11-22 19:28:08

枚举“成员”可以具有基础“值”。从 int 的继承告诉您该值将采用什么类型。

An enum "member" can have an underlying "value". The inheritance from int tells you what type the value will take.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文