What is missing in the other answers is that enums are of an integer base type. You can change the default value from int to any other integer type except char, for example:
enum LongEnum : long { foo, bar, }
You can explicitly specify and implicitly the base type, which is useful in switch statements. Beware that any value of the base type can be enumerated, even if the enumeration does not have a member with the corresponding value. Therefore, always using the default switch is a good idea. BTW, .NET itself allows even overridden floating point values, but you cannot define them in C #, although I think you can still use them (except for the switch).
In addition, using enumerations gives you more type safety. If you intend to use, for example, int constants as method parameters, then I could call the method with any int value. Of course, through casting, this can also happen with transfers, but this will not happen by accident. Worse is the ability to confuse the order of parameters.
void method(int a, int b) {...}
If the constant A can go only to a, and the constant B can go only to b, then using two different types of enumerations will detect any abuse at compile time.
Johannes Mar 05 '09 at 16:22 2009-03-05 16:22
source share