Space allocated to an enumeration

  • Thread starter Thread starter ....DotNet4Ever....
  • Start date Start date
D

....DotNet4Ever....

I don't know about the internals of .net code generation, quite frankly the
very last time I bothered to look at generated code was some 15 years ago
when I worked with embedded systems.

Now I have been wondering about .NET enumeration items and the space that it
is allocated in memory for them. For example, if I create this enum:

enum TestEnum
{
One = 10000,
Two,
Hundred = 10100
}

does it allocate three (3) items, or does it allocate a memory block with
10,100 items of which only three are actually used? In other words, is it a
look up table or an indexed table?
 
....DotNet4Ever.... said:
I don't know about the internals of .net code generation, quite frankly
the very last time I bothered to look at generated code was some 15 years
ago when I worked with embedded systems.

Now I have been wondering about .NET enumeration items and the space that
it is allocated in memory for them. For example, if I create this enum:

enum TestEnum
{
One = 10000,
Two,
Hundred = 10100
}

does it allocate three (3) items, or does it allocate a memory block with
10,100 items of which only three are actually used? In other words, is it
a look up table or an indexed table?


Hi,

This is a type definition, it won't allocate anything.

Enums are value types, and as i know their size is determined by the enums
underlying type (what is int by default).
So when You declare a variable of a given enum type it will consume the size
of its underlying type.

Hope You find this useful.
-Zsolt
 
.....DotNet4Ever.... said:
I don't know about the internals of .net code generation, quite frankly
the very last time I bothered to look at generated code was some 15
years ago when I worked with embedded systems.

Now I have been wondering about .NET enumeration items and the space
that it is allocated in memory for them. For example, if I create this
enum:

enum TestEnum
{
One = 10000,
Two,
Hundred = 10100
}

does it allocate three (3) items, or does it allocate a memory block
with 10,100 items of which only three are actually used? In other words,
is it a look up table or an indexed table?

Neither. As miher explained, this is a type declaration. This *does*
actually take up space, in the assembly's metadata, but that's just a few
bytes to associate the tags with their values. So in that regard, it's just
three items.

At runtime, values of this enumeration type will take up 4 bytes, as by
default enumerations are int-based. You can explicitly specify the
underlying integer type to use with "enum TestEnum : byte" (for example).

You may have been thinking of testing on an enumeration in a switch
statement, which is another matter altogether. The compiler will use a
combination of arithmetic, if-statements and the special switch opcode
(which only works on consecutive values) to produce a small and fast jump
table, usually linear in size to the number of cases.

On my machine, the following code:

TestEnum e;
switch (e) {
case TestEnum.One: DoOne(); break;
case TestEnum.Two: DoTwo(); break;
case TestEnum.Hundred: DoHundred(); break;
}

Produces code which looks like this (paraphrased):

int i = ((int) e) - 10000;
on i goto DoOneLabel, DoTwoLabel;
if ((int) e == 10100) goto DoHundredLabel;
goto OutLabel;
DoOneLabel:
DoOne(); goto OutLabel;
DoTwoLabel:
DoTwo(); goto OutLabel;
DoHundredLabel:
DoHundred();
OutLabel:

C# doesn't have a "on X goto" statement like the early Basic dialects had,
but pretend it does.

If you're interested in how various .NET constructs translate to byte code,
download Reflector (http://www.red-gate.com/products/reflector/) and try it
out. If you want to go even further and see what *machine* code is generated
after the byte code is compiled, install the Debugging Tools for Windows
with the sos.dll extension.
 
Thanks,
My thinking was a bit deeper than Z's because an enum always takes up
space somewhere in memory for example as a look-up table. Jeroen's answer is
therefore what I was looking for. Thanks to both anyway.

Emil
 
Back
Top