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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…