c# - When to use enums, and when to replace them with a class with static members? -


It has recently come to me that the following (sample) calculations ...

 < Code> enum color (red, green, yellow, blue)  

... appears to be replaced by a more type-protected class:

 < Code> color of the square {private color () {} public static text only color red = new color (); Colorful green = new color for public static reading (); Public Static Text Only Color Yellow = New Color (); Public Static Text Only Color Blue = New Color (); } With "type-safe", I mean if  color  was an enum, but if  color  was the above classes: 

  Var nonsenseColor = (color) 17; // works if the color is an enum  

two questions:

1) Is this method a widely accepted name

2) In which case should any eneme be used, and when a class would be more appropriate? For the light state information, enums are great, for example, your color enum (excluding blue)

  [flag] public enum lightcolors {unknown = 0, red = 1, yellow = 2, Green = 4, green_arrow = 8}  

The current state of light can be set as:

  light color c = LightColors.red | LightColors.green_arrow;  

and inquire as:

  if ((& amp; lightcollers.rd) == lightcolors.rd) {// do not drive} If ((& LightColors.green_arrow) == Light Colors. Green_arrow) {// Turn}  

Static class color member able to support this multiple state without additional functionality Will be

However, members of the stable class are generally excellent for the objects used. System.Drawing.Color Members are great examples because they represent a known-name color Who are obscure creators (unless you know your hex colors). If they were implemented as energy, then every time you want to use the value in the form of color, then something like this must be done:

  colors c = colors.red ; Switch (C) {Case color.red: Return System. Drawing Collar FORARBB (255, 0, 0); break; Hair colorful Green: Return System. Drawing Collar From ARGB (0, 255,0); break; }  

So if you have got an enumen and discover that your constant one switch / case / if / other / whatever object you want to get, Would like to use. If you are just asking for some situation, then I will be with you. In addition, if you have to pass data to insecure fashion anomes, then probably will be better than a serialized version of your object.

Edit: @stakx, I think that stumbling upon you is also important in responding to Anton Anton's post, and this is complexity or more important, which Is that complicated for it?

From the consumer's perspective, I like the system very much. Drawing After being a member of the stable class, writing all of this from a producer's point of view, however, it would be painful to write all that. So if other people are going to use your code, you can save them a lot by using members of the stable class, even if they can take you 10 times for writing / testing / debugging. However, if it's just easier for you to use emium and cast / need to be done as needed.


Comments