Memory allocation for enumerated types in .NET

190 views Asked by At

Now, when I used to work a lot with embedded systems and severe memory constraints I remember we had to be extremely conscious about the types used and how they were used. Now, it is typical of Windows programmer to waste space simply because you have several GB of memory. In any case I prefer not to waste when it is not really necessary because if every app (and the OS) does it, then it all accumulates and you end up with a sluggish PC.

Having said so, I was thinking of enumerated types in C# or actually .NET because it is irrelevant which language you use. For example, if I define an enumerated type like this:

enum FreeEnumeration {
    One,
    Two,
    Three
}

in physical memory when allocated, would it occupy exactly the same amount of bytes as this:

enum SpreadEnumeration {
    One = 1000,
    Two = 2000,
    Three = 3000
}

For those that don't get the point, in some (early) compilers the enumerations were usually stored in memory as lookup tables, so the second one would have taken some 3000 slots.

Now, I am inclined to believe that in .NET they are not lookup tables and would occupy the space of 3 integers plus whatever other info. Or am I mistaken? Just curious, I don't want to fill up memory with zeroes just because I want to be lazy.

1

There are 1 answers

0
Travis On BEST ANSWER

The size of an enum is just an int (by default). See https://stackoverflow.com/a/20944766/396746 for a bit more detail.

The size of the enum reference structure is just the number of elements in it.