Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enum types stored as ints in C#?

Tags:

c#

Are enum types stored 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
}
like image 818
JamesRedcoat Avatar asked Jul 23 '11 16:07

JamesRedcoat


People also ask

Are enums ints in C?

enums are not always ints in C.

Are enums always integers?

Enumerations are integers, except when they're not - Embedded.com.

Where are enum variables stored in C?

They are not stored in the enum variable itself, but are stored as any other numeric constant: either as part of the machine code itself (segment often called . text ), or in a separate read-only segment (segment often called . rodata ).

What is the data type of enum in C?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.


1 Answers

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.

like image 197
ChrisF Avatar answered Oct 17 '22 17:10

ChrisF