Associating enums with strings in C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Although the title says C, this question usually appears in a C# or .NET context, where enums are common and developers want readable labels for UI, logs, or API output. An enum stores numeric values, but you can map those values to strings in several ways depending on whether you need simplicity, performance, or localization.
Start with ToString
If the enum member name is already the text you want, use ToString(). It is the simplest option and requires no extra mapping.
This prints Moderator. That is fine for internal logs or debugging, but it becomes limiting once you need spaces, friendly wording, or translated text.
Use a switch for Explicit Mappings
For small enums, a switch expression is often the clearest approach. It is fast, easy to read, and checked by the compiler.
This is a strong default when the mapping is part of application logic and not meant to be configured externally.
Use Attributes for Metadata
If you want the string to live next to the enum definition, attributes are useful. A common pattern is DescriptionAttribute.
The advantage is that the mapping stays close to the enum declaration. The tradeoff is that reading the attribute usually uses reflection, which is slightly more complex than a direct switch.
Use a Dictionary for Configurable Mappings
If the strings change often or come from configuration, a dictionary can be a better fit than attributes.
This approach works well when the mapping belongs to a specific feature rather than to the enum type itself.
Which Approach Should You Choose
Use ToString() when the enum name already matches the final output. Use a switch when you want explicit, compile-time-safe labels. Use attributes when the display text should live with the enum definition. Use a dictionary when the mapping is feature-specific or may be replaced at runtime.
For localization, do not hard-code every translated string in attributes unless your project is small. In larger applications, enums often map to resource keys, and the UI layer resolves those keys through the localization system.
Common Pitfalls
The biggest pitfall is assuming enum names are user-facing text. PaymentDeclinedByFraudRules may be a good internal identifier, but it is poor UI copy.
Another mistake is forgetting to update the mapping when a new enum value is added. A switch expression helps here because the compiler can warn when you are not handling all members.
Reflection-based attribute lookup can also be overused. It is fine for admin screens or forms, but if you call it in very hot code paths, caching the result is better.
Finally, avoid mixing several mapping styles for the same enum unless there is a clear reason. If one screen uses ToString(), another uses attributes, and a third uses a dictionary, inconsistent labels will creep into the product.
Summary
- Enums do not store custom strings directly, so you need a mapping strategy.
- '
ToString()is simplest, but only returns the enum member name.' - A
switchexpression is a clear and fast way to produce friendly labels. - Attributes keep display text close to the enum definition.
- Dictionaries work well when labels are feature-specific or configurable.

