Enums and Constants. Which to use when?
What's missing from the other answers is that enums have an integer base type. You can change the default from int to any other integral type except char like:
enum LongEnum : long {
foo,
bar,
}
You can cast explicitly from and implicitly to the the base type, which is useful in switch-statements. Beware that one can cast any value of the base type to an enum, even if the enum has no member with the appropriate value. So using always the default section in a switch is a good idea. BTW, .NET itself allows even floating point valued enums, but you can't define them in C#, although I think you can still use them (except in switch).
Furthermore, using enums gives you more type safety. If you intend to use e.g. int constants as method parameters, then I could call the method with any int value. Granted, via casting it can happen with enums, too, but it won't happen accidentally. Worse is the possibility to confuse the order of parameters.
void method(int a, int b) {...}
If constant A only may go into a and constant B only may go into b, then using two different enum types will uncover any misuse during the compilation.
A constant is a language feature which says that the variable won't change value (so the compiler can do optimisations around that knowledge) where an enum is a specific type.
Constants can be any data type but an enum is an enum.
I use an enum any place where you could have a number of options and want to improve readability of the code. i.e. you could have trace levels as an int with values 0, 1, 2 or as an enum as error, warning and info.
Enum's also have the ability to be used as bitwise operators, i.e. FontStyle.Bold | FontStyle.Italic would give you bold and italic fonts.
Use enums when you want to define a range of values that something can be. Colour is an obvious example like:
public enum Colour
{
White,
Red,
Blue
}
Or maybe a set of possible things like: (Example I stole from here as I'm lazy)
[FlagsAttribute]
enum DistributedChannel
{
None = 0,
Transacted = 1,
Queued = 2,
Encrypted = 4,
Persisted = 16,
FaultTolerant = Transacted | Queued | Persisted
}
Constants should be for a single value, like PI. There isn't a range of PI values, there is just PI.
Other points to consider are:
- a: Constants don't necessarily indicate a relationship between the constants, whereas an enumeration indicates that something can be one of the set defined by the enum.
- b: A defined enumeration can help you with type checking when used as an argument. Constants are just values, so they don't provide any additional semantic information.