开发者

Are enum types stored as ints in C#?

Are enum types sto开发者_StackOverflowred as ints in C#?

Will the enum below be represented as 0, 1, 2?

If not, what's the lightest way to define an enum?

public enum ColumnType
{
    INT,STRING,OBJECT
}


From the MSDN

The default underlying type of enumeration elements is int.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

So your assumptions are correct. Enums are stored as ints and your example will be represented as 0, 1, 2. However, you shouldn't rely on this and always refer to them by their assigned name just in case someone overrides the default.


Yes, an enum is stored as an int by default., as indicated here.

You can also specify the underlying type, e.g.:

enum Foo : short
{
    Bar,
    Baz
}

Although that usually isn't necessary.


By default yes:

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

The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. 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. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.


The default underlying type for an enum is an int, but different types can be used explicitly. For example, you can use byte:

enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

See http://msdn.microsoft.com/en-us/library/sbbt4032.aspx for details.


Every enumeration type has an underlying type, which can be any integral type except char. Yes your enum be represented as 0,1,2 by default and it is the lightest way to define an enum

How ever you define enum by defining starting value as your requirement and assigning integral values explicitly to each of your enum member as well


The point about underlying types is already well-covered, so I'll add a different context:

At the IL / runtime level, instances of enums do not exist. They only exist:

a: to the compiler b: to the runtime as a boxed value c: in metadata (parameter/field/variable declarations)

At all other times, they are purely the integer-types values (most commonly int). The compiler uses enum variables (etc) for static analysis, overload resolution, etc but the opcodes it emits are identical to those it would have emitted for constant / literal integer values; i.e.

SomeEnum foo = SomeEnum.WithValueOne;
int bar = 1;

will have identical IL (except for the variable type, perhaps).

The only time it gets interesting is if emitting a box operation, or a call like .ToString() etc.

So: as long as you don't box them (store them in an object field/etc), then there is (at runtime) no significant difference between an enum or an integer when it comes to storage or performance (unless you use .ToString(), etc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜