Enumerations are great for mild status information. For example, your color enumerator (excluding blue) would be good for querying the status of a traffic light. True color along with the whole concept of color and all its baggage (alpha, color space, etc.) It does not matter just what state the light is in. Also, changing your listing a bit to represent the state of the traffic light
[Flags()] public enum LightColors { unknown = 0, red = 1, yellow = 2, green = 4, green_arrow = 8 }
The current state of lighting can be set as:
LightColors c = LightColors.red | LightColors.green_arrow;
And is requested as:
if ((c & LightColors.red) == LightColors.red) {
Elements of a class of a static class will be able to support this plural state without additional functions.
However, static class members are great for commonly used objects. System.Drawing.Color elements are great examples, because they are colors of famous names that have obscure constructors (unless you know your hexadecimal colors). If they were implemented as enumerations, you would have to do something like this every time you wanted to use a value as a color:
colors c = colors.red; switch (c) { case colors.red: return System.Drawing.Color.FromArgb(255, 0, 0); break; case colors.green: return System.Drawing.Color.FromArgb(0,255,0); break; }
So, if you have an enumeration and you find that you constantly do the / case / if / else / what switch to get the object, you can use the static members of the class. If you are only asking for the status of something, I will stick with the listings. Also, if you need to transfer data in unsafe mode, enums are likely to survive better than the serialized version of your object.
Link to an old post from this forum: When to use enumerations and when to replace them with a class with static members?
hm1984ir
source share