In C, is there a nice way to track the number of elements in an enum? I've seen
enum blah { FIRST, SECOND, THIRD, LAST };
But this only works if the items are sequential and start at zero.
Enum.GetNames(typeof(INFORMATION)).Count() It will be helpful. Even though you will increase decrease values in enum It will give correct result.
The size is four bytes because the enum is stored as an int . With only 12 values, you really only need 4 bits, but 32 bit machines process 32 bit quantities more efficiently than smaller quantities.
An ENUM column can have a maximum of 65,535 distinct elements. If you retrieve an ENUM value in a numeric context, the column value's index is returned.
If you don't assign your enums you can do somethings like this:
enum MyType { Type1, Type2, Type3, NumberOfTypes }
NumberOfTypes will evaluate to 3 which is the number of real types.
I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:
enum blah { FIRST = 128, SECOND, THIRD, END }; const int blah_count = END - FIRST;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With