What is the point of using int as enums

Why do many people list this way:

public enum EmployeeRole { None = 0, Manager = 1, Admin = 2, Operator = 3 } 

instead of doing:

 public enum EmployeeRole { None, Manager, Admin, Operator } 

Are there any advantages?

+7
source share
6 answers

Are there any advantages?

maintainability. Say these integer values ​​are stored in a database. You do not want to add a new value to the enumeration in the future and change the values, because you insert the value in such a way as to shift unspecified values.

Clarity. Evidence is good. Say again that we are reading integers from the database from some old application. Thus, the codes already have a specific meaning, and we want to explicitly build them. We could say

 public enum EmployeeRole { None, Manager, Admin, Operator } 

and maybe it exactly matches the outdated specification or we could say

 public enum EmployeeRole { None = 0, Manager = 1, Admin = 2, Operator = 3 } 

and now it’s easier to read if we are in line with the outdated specification.

+4
source

This is useful when you have a contract elsewhere. If you store an enumeration in a database, you want to explicitly enter numbers to make sure that you do not accidentally renumber the enumeration by inserting a new element in the middle.

+2
source

It explicits defines the value, and does not allow the compiler to process it at compile time. In case you provided, it really makes no sense, except to be readable and clearly defined. This does not interfere and leads to the same MISL as without explicitly specifying them. However, in cases where your enumerations refer to certain values ​​that are not automatically added, as indicated above, such an explicit definition is very convenient.

 public enum MyEnum { First = 1, Second = 2, Eleventh = 11 } 
+1
source

This helps to avoid changing these supposedly constant values ​​just because someone is rearranging the class. Let's say you have a new employee who decides that "None" should go at the end of the list:

 public enum EmployeeRole { Manager, Admin, Operator, None } 

Well, if you only ever accessed these values ​​directly from EmployeeRole. Be that as it may, this is not a huge deal. But most of the enumerations I saw at some point translate to an integer value when they are stored in the database. This means that all of your No items in the repository have just been converted to Manager.

The same problem would arise if someone simply inserted a new EmployeeRole between, say, the administrator and the operator.

Another advantage arises when you do not think that there should be an appropriate default value for your listing. For example, if someone forgot to display the EmployeeRole field in ORM, objects pulled from the repository will always have the None role (0 is always the default value for enumerations). Depending on how your software handles None , this kind of error may not appear for some time. But if you do this:

 public enum EmployeeRole { Manager = 1, Admin = 2, Operator = 3 } 

... and then combine it with fault tolerant methods, you can quickly catch errors when an invalid value of "0" was specified:

 public RightsManager GetByEmployeeRole(EmployeeRole role) { Require.That(role.IsDefined()); // throws an exception if role is not defined. // find the rights manager for this role. } 
0
source

For example, when you store values ​​in a database, it recommends a fixed mapping between numbers and symbolic values. If you do not specify the numeric values ​​explicitly, the compiler will indicate them sequentially, so if you enter a new one, you will have a mismatch.

0
source

I see two main advantages:

  • Providing a value that is implemented for something else (e.g., database value, interval, etc.). Please note that you do not need to specify ordered values. For example, it can be 1, 23, 2 and 4.
  • This is more readable if you need numerical values.
0
source

All Articles