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. }
Stripling warrior
source share