Enums, or enumerated types, are a list of constant values with developer-friendly names. They are used in programming and are often used to establish a set of predefined values that a variable can take.
Define a list of values
Enums are a list of values, each of which is always unique. You cannot have two values in the enum with the same name. This makes them very useful for defining all possible value states that a function can handle.
For example, you could have an enumeration called “Fruits”
stringto display this variable – a
stringcan have infinite possible values, while this enumeration can have only three.
You can then use this enumeration in your code. For example, check if the user input type equals an Apple, or even pass it as an argument to one
switch/case statement, do something specific for each type of possible value.
Under the hood, enums themselves have values. This depends on the language implementation, but in C # enums have integers by default. The default values start at 0, 1, 2, 3, etc., but this can be changed manually to set each enum item to a specific value. For example HTTP status codes:
Which can then be converted back and forth to the value and enumeration type, using basic casts:
Enums are not only useful for defining sets of items, they are also great for the developer experience. Even if you convert to a string or int before submitting data to an API, using enums in your codebase provides more flexibility and leads to cleaner code.
Having autocomplete drop-down lists for a list of possible values not only helps you, but also helps anyone working on your codebase in the future. You don’t want to maintain code that accepts strings as arguments and does arbitrary things based on the input. Using an enum instead strictly defines the application behavior.
Disadvantages of Using Enums
The main downside to enums is when they leave your codebase and lose their special meaning. For example, if you have an API to which you store data and send data to, you must first serialize the enum, which will probably take the underlying number 0, 1, 2, etc. by default. Some languages support enums with an underlying string of values or custom bulleted serialization rules, which can help alleviate this problem.
This can also be a problem if your enums change. Once you start using an enumeration, you can’t change its order, just add new items at the end. Otherwise, data saved with the old version of the enumeration would become obsolete and unreadable.
Enum types as flags
Another common use for enums is to define bitwise flags. This is quite an advanced concept, but in fact each enumeration value represents a single Boolean value. Together, the entire enumeration can be stored in a single integer and used to perform quick searches for Boolean data.
This works because each enumeration value is set to a different bit in the underlying number. For binary, this means that the enumeration values will be 0, 1, 2, 4, 8, 16, etc. You can then add them together to display a list of Boolean values.
Why are you doing this instead of multiple booleans? First, it saves space, which can be an advantage in some situations (where you store a lot of them). But more importantly, it is very fast to access any value, especially when you have access to multiple values. For example, if you want to check if it’s the weekend, you can check if it’s it
Saturday | Sunday, all of the same byte loaded into memory. The CPU only needs to fetch one item to get a list of all flag states.