Enum inheriting from int

你。 提交于 2020-01-10 03:27:48

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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).




回答5:


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.




回答6:


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.




回答7:


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




回答8:


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.



回答9:


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



来源:https://stackoverflow.com/questions/6348924/enum-inheriting-from-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!