I recently had to solve the problem of converting strings into enums in C#. There are a few different solutions you can implement depending on your requirements. This article aims to show you some of these solutions and when to use each one.
I recently had to solve the problem of converting strings into enums in C#. There are a few different solutions you can implement depending on your requirements. This article aims to show you some of these solutions and when to use each one.
Method 1: Enum.Parse Method
This is probably one of the simplest methods as it is built into .NET. It allows you to convert a string into an enum by matching the given string to the enum item with the same name and then returning that enum item.
It's important to note that the string comparison is case-sensitive by default but can be configured to be case-insensitive.
Advantages
Easy to remember and read
Can use case-insensitive comparison
Can return a typed enum using the generic type overload ( Enum.Parse<TEnum>() )
Disadvantages
Need to implement error handling with try-catch (catch the ArgumentException exception for when the specified enum item is not found)
The string has to match the enum's item name (i.e. you cannot match on description or other attributes in the enum)
Method 2: Enum.TryParse Method
This method works the same as the previous method: it retrieves the enum item with the same name as the specified string. However, this method also takes care of error-handling for us making it much simpler to use.
Just like the previous method, this method is also case-sensitive by default but can be configured to be case-insensitive.
Advantages
Easy to remember and read
Can use case-insensitive comparison
Can return a typed enum using the generic type overload ( Enum.TryParse<TEnum>())
Manages error-handling and returns a bool indicating if the conversion was successful or not.
Disadvantages
The string has to match the enum item's name (i.e. you cannot match on description or other attributes in the enum)
Method 3: Using Reflection
This method is by far the most complex but gives us a lot more control over retrieving the correct enum item based on the given string. We will use reflection to retrieve the correct enum item using the DescriptionAttribute assigned to each enum item. This attribute is powerful because our string does not have to match the name of the enum item exactly.
In the example below, we are implementing reflection inside a static method so that we can then call this static method to retrieve the correct enum item based on the given string.
Advantages
Allows more flexibility in how an enum item should be picked based on a given string (you don't have to use the DescriptionAttribute only)
Can use case-insensitive comparison
Can return a typed enum using the generic type overload ( Enum.TryParse<TEnum>())
Disadvantages
Requires writing additional methods
Closing
I hope you found this article helpful. If you have any other ideas as to how you can convert a string into an enum item please mention it in the comments below. Otherwise, if you have any questions you can also leave them below and I'll get back to you.