Convert String to Enum in C# |
If you're a C# developer, you might have come across situations where you need to convert a string to an enumeration type. Enumerations, or enums for short, are a way to define a set of named constants in C#. They are useful when you need to work with a fixed set of values that don't change frequently.
In this blog post, we'll discuss how to convert a string to an enum in C#. We'll cover the basics of enums and string parsing, and provide code examples to help you get started.
What is an enum in C#?
In C#, an enum is a value type that represents a set of named constants. Enums are defined using the "enum" keyword, followed by the name of the enumeration and a list of comma-separated constant values.
For example, let's say you have an application that deals with different types of fruits. You could define an enum like this:
Here, we've defined an enum called FruitType with three named constants: Apple, Banana, and Orange. These constants are assigned integer values by default, with Apple being 0, Banana being 1, and Orange being 2.
Converting a string to an enum in C#
Sometimes, you might have a string value that represents one of the named constants in an enum. For example, you might have a string that contains the word "Banana". To use this value in your C# code, you'll need to convert it to the corresponding enum constant.
To do this, you can use the Enum.Parse method. This method takes two parameters: the type of the enum, and the string value to parse.
Here's an example that shows how to convert a string to an enum in C#:
In this example, we have a string variable called fruitString that contains the value "Banana". We then use the Enum.Parse method to convert this string to the corresponding enum constant. The typeof(FruitType) expression specifies the type of the enum, and we cast the result to a FruitType variable.
If the string value does not match any of the named constants in the enum, Enum.Parse will throw an ArgumentException. To handle this case, you can use the Enum.TryParse method instead. This method has the same parameters as Enum.Parse, but it returns a boolean value that indicates whether the parsing was successful or not. If parsing was successful, the parsed value is returned as an out parameter.
Here's an example that shows how to use Enum.TryParse:
In this example, we have a string variable called fruitString that contains the value "Pear", which is not a named constant in our FruitType enum. We then use the Enum.TryParse method to try to parse the string value. If parsing was successful, the parsed value is returned as an out parameter, and the success variable is set to true. Otherwise, success is set to false.
Converting a string to an enum in C# is a common task that can be accomplished using the Enum.Parse or Enum.TryParse methods. These methods make it easy to work with enums and string values in your C# code. By following the examples in this post, you'll be able to quickly and easily convert string values to enums, and handle cases where the string value does not match any of the named constants in the enum.
One important thing to keep in mind when working with enums is that the integer values assigned to the named constants are implementation details that can change over time. If you rely on the integer values in your code, your code may break if those values change.