enum type

T

Tony Johansson

Hello!

Here we have an enum declaration
enum OnOff {On,Off}
One thing that is strange is why the default type for containing items in an
enum is of type int.
It should be enough in 99,9% to use byte.
I know I can use
enum OnOff : byte {On,Off}but the default type should be byte to save
memory.

So is there a reason why the containing items in an enum is of type int ?

//Tony
 
J

Jeroen Mostert

Tony said:
Here we have an enum declaration
enum OnOff {On,Off}
One thing that is strange is why the default type for containing items in an
enum is of type int.
It should be enough in 99,9% to use byte.

Accessing bytes is slower than accessing ints. All registers and memory
accesses are int-sized (at least).
I know I can use
enum OnOff : byte {On,Off}but the default type should be byte to save
memory.
The memory gains would be insignificant. Unless you're storing thousands of
these, it doesn't make sense to use a byte. Packing bytes leads to reduced
performance and increased code size for extracting the results; only if you
have a lot of them is this offset by the memory and performance gains from
having to access less memory.
 
F

Family Tree Mike

Tony said:
Hello!

Here we have an enum declaration
enum OnOff {On,Off}
One thing that is strange is why the default type for containing items in an
enum is of type int.
It should be enough in 99,9% to use byte.
I know I can use
enum OnOff : byte {On,Off}but the default type should be byte to save
memory.

So is there a reason why the containing items in an enum is of type int ?

//Tony

If you are using your enum as flags, that can be "or'ed" together, then
a byte would limit you to eight flags. That is a limit that can be hit
rather easily.
 
P

Peter Duniho

[...]
enum is of type int.
It should be enough in 99,9% to use byte.

This is not true. Using "byte" in many situations could cause problems,
not the least of which the question of the compiler picking different
underlying types based on the contents of the enum (so, making it much
easier to break things if the enum is changed, pushing it past the "byte"
threshold).
I know I can use
enum OnOff : byte {On,Off}but the default type should be byte to save
memory.

So is there a reason why the containing items in an enum is of type int ?

In addition to what Jeroen wrote, the bottom line is that it's like this
because the C# spec says it is.

The language philosophy is not to optimize, but rather to provide defaults
that are broadly compatible and useful. As you say, you can force the
type to "byte" if you need to, and otherwise constraining the underyling
type to "byte" by default would introduce more issues than it would solve.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum TypeConverter 3
Enum Question 1
Enum Extentions 7
enum with underlying type 1
about enum 1
I have small probalem 4
number of values in enum 2
same Enum and property 12

Top