How many entries can an enum in .NET have?
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
You can define enums based on any of the integer types (and thus have as many entries as available for that integer size).
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.
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
{
...
}
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
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!!!)