C # Enum or int constants

I have a group of values ​​that represent a state (ON, OFF, READY, ...). These values ​​are also stored in the database as an int field, so I wonder if the best practices will say that this is an enumeration or just a bunch of const int types in the class.

Enum seems like a natural approach for reading / coding a person, but it seems to hide the fact that it has a value for which integer values ​​correspond to values ​​(or values ​​obtained from the database will be created in an incorrect state). Someone may come later and add a new value to the enumeration or something else and throw it all away.

What is the best approach?

+6
source share
6 answers

I think enum is still the best choice for readability. However, since its values ​​are stored in the database, you must explicitly specify the values:

 enum State { On = 1, Off = 2, Ready = 3}; 
+10
source

How does someone add a new enum value other than a new int constant?

Remember that you can set an enumeration to a specific integer value.

Hug readability!

As an added bonus, you can now use strong typing to prevent the use of fraud, such as

 Widget.state = 474; 

Where 474 does not match the state in your database.

+3
source

I would go with the enumeration and make sure that it is very well documented which table matches the enumeration.

The danger associated with a set of int constants is that the type does not indicate that the values ​​are valid, and someone can very easily assign any old value. An attacker of your code can still do this with a throw, but you cannot stop some people from shooting by themselves ...

+1
source

If your values ​​rarely change, or if you have no problems editing and recompiling the code, then be sure to use an enumeration with an explicit declaration of integer values ​​for each. Otherwise, I would create an object wrapper that retrieves the values ​​from the database (you can always cache the values ​​a little if you are concerned about performance). Then do all your comparisons with the wrapper of this object. If you cannot guarantee that the values ​​in the database will not change without updating the code using an enumeration in your code, this will be simply volatile and risky. If you are really adventurous and want to get into the emitting code, you can do quite interesting things with this.

+1
source

You can define the values ​​in Enum yourself, i.e.

 enum STATE {ON=2, OFF=9, READY=14}; 
0
source

I would say Enums. They provide you with a machine format in a readable format. Make sure you document it well.

 ///Summary /// About State Enum ///Summary enum State : int { ///Summary /// About Off Enum Value ///Summary Off = 0, ///Summary /// About On Enum Value ///Summary On, ///Summary /// About Ready Enum Value ///Summary Ready }; 

No need to assign a value to each member. Start at 0 and rest will automatically increase.

Since your listing is on / off, you can use it in a logical way. 0 means False or off and 1 means true or on.

You can also convert your enum to int as

 int value = (int)State.On; // value will have 1 

Save the value in the database as an int. and when extracting you can do it like this:

 State st = (State)int.Parse(mydatabasevalue); 
0
source

Source: https://habr.com/ru/post/915800/


All Articles