tags:

views:

358

answers:

6
+3  Q: 

.NET enum size?

How many entries can an enum in .NET have?

+3  A: 

18,446,744,073,709,551,616 (the max value of an unsigned 64-bit integer).

Enums can be based on:

  • Byte
  • SByte
  • Int32
  • UInt32
  • Int16
  • UInt16
  • Int64
  • UInt64
Rex M
I can't believe I'm writing this, but... it should be one greater, 18,446,744,073,709,551,616 (0 to 18,446,744,073,709,551,615 inclusive).
Michael Petrotta
You know, I can't believe you wrote that either (I suspect cut'n'paste) but +1 for being anal^H^H^H^Hpedantic :-)
paxdiablo
Who keeps Int64.MaxValue in their head?
Rex M
It struck me odd that the value was odd. From there, the magic of Google and MSDN. But Rex, I'd honestly be flattered to be thought of as a person who kept Int64.MaxValue in my head.
Michael Petrotta
I keep every power of two up to 262144 but, beyond that, I need a calculator (or, more usual, just use 1<<power).
paxdiablo
I'm happy knowing 2 to 16th power, but then again, I need spare memory free for Pi ;-)
Si
+2  A: 

You can define enums based on any of the integer types (and thus have as many entries as available for that integer size).

Brian
+12  A: 

I'm pretty certain the underlying type is an integer type (not necessarily an int). This page here states that:

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong. The underlying type specifies how much storage is allocated for each enumerator.

and:

Just as with any constant, all references to the individual values of an enum are converted to numeric literals at compile time.

So I would suggest the only limitation will be the range of long/ulong (well into the billions) and the allowable symbol space in the compiler for enumerations (you'll most likely strike that limitation first if you're actually creating truly massive enumerations).

If you specify a smaller type for your enumeration (e.g., short), the range will be reduced accordingly.

paxdiablo
It could be a valid question if he is using [flag] enums.
tster
+2  A: 

It depends on the underlying type of the enumeration (see below). The number of max entries would then be the maximum number of byte, long, etc.

enum Stuff : byte
{
...
}

would have a different number of max entries than

enum Stuff : long
{
...
}
Taylor Leese
+3  A: 

Keep in mind if bitwise operations are desired, the number of distinct non-combination values depends on the number of bits in the integer type used.

For example, a 32 bit integer has 32 possible values and a 64 bit integer has 64 possible values. There is more on this here: http://stackoverflow.com/questions/1425224/flagsattribute-negative-values

No longer a user
+2  A: 

Since they don't need to be unique... as many as you can get away with until you break the compiler... but it sounds like you are already abusing them...

For info:

enum Foo
{
    A = 1,
    B = 1,
    C = 1
}

(this shouldn't be a problem in real world usage!!!)

Marc Gravell