Get Enum from Description attribute
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, an enum member can carry a DescriptionAttribute to expose a human-readable label. Going from enum value to description is straightforward; going the other direction requires reflection, because the description is metadata attached to the enum field.
Define the enum with descriptions
A typical enum looks like this:
If a user selects "Ready to ship" from a UI, your code may need to convert that description back into OrderStatus.ReadyToShip.
Read the enum from the description
The core approach is:
- iterate over enum values
- inspect the corresponding field
- read the
DescriptionAttribute - compare its text with the input
This generic helper works for any enum type that uses DescriptionAttribute.
Return a safe result instead of throwing
If the input may be invalid, a Try-style API is often safer than throwing exceptions.
That is useful for parsing user input, query strings, or imported files where failure is expected occasionally.
Cache results if the lookup is frequent
Reflection is fine for occasional lookups. If the code runs repeatedly in a hot path, cache the mapping once.
Then a lookup becomes:
This is much faster when the same mapping is used many times.
Description strings versus stable identifiers
Be careful about using descriptions as a system-level identifier. Description text is often meant for display and may change for wording or localization reasons.
Use description-based parsing when:
- the enum is tightly tied to one display language
- the mapping is local and controlled
- users or configs truly use the description text
If the value must stay stable across versions and languages, a dedicated code string is often a better design than relying on display-oriented descriptions.
Common Pitfalls
The biggest mistake is assuming Enum.Parse can read the description text automatically. It cannot. Enum.Parse works with enum names, not with DescriptionAttribute.
Another issue is forgetting that some enum members may not have a DescriptionAttribute. A robust helper should decide whether to ignore those members or fall back to the enum name.
Developers also overlook performance. Reflection is perfectly fine for occasional parsing, but repeated lookups should usually be cached.
Finally, description text can change over time. If external systems depend on it, a wording change can break parsing unexpectedly. Use stable identifiers when long-term compatibility matters.
Summary
- Converting from
DescriptionAttributeback to an enum requires reflection. - A generic helper can inspect enum fields and compare description text safely.
- '
TryFromDescriptionis often better than throwing for user-driven input.' - Cache the mapping if the lookup happens frequently.
- Do not treat human-facing description text as a stable integration contract unless that tradeoff is deliberate.
Related reading
- Get log4net log file in C
- Get number of digits in an unsigned long integer c
- Get output parameter value in ADO.NET
- Get sum of two columns in one LINQ query
- Get the .NET assembly's AssemblyInformationalVersion value?
- Get the previous month's first and last day dates in c
- Get the sum of powers of 2 for a given number c
- Get Timezone Offset of Server in C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.