3 ms·
In C#, enums basically add typing to a predefined set of entities or statuses. This is why they basically are just numbers under the hood. Practically, though,
by gwbas1c 5y ago
In C#, enums basically add typing to a predefined set of entities or statuses. This is why they basically are just numbers under the hood.
Practically, though, they are important when dealing with serialization: Writing state to disk, database, or over the wire. Because they quickly typecast to a number or a string, you don't have to deal with the complexity of serializing or deserialzing.
Having recently inherited a C# codebase where enums weren't used, what I can say is that "polymorphic classes" to represent a predefined set of things is a smell. This manifests itself in cases where we had tables with a limited set of constants, and were loading them through joins. It was much more efficient to remove the join and turn the foreign key into an enum. In another case, the "object" is fully serialized in a contract to JSON. It's much more efficient to just serialize a single value in the contract as well.