Convert String to Enum in C#
Convert String to Enum in C#
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Ravi Vishwakarma
11-Jun-2024You can convert a string to an enum using the Enum.Parse() method. Here's how you can do it:
This code snippet demonstrates converting the string "Tuesday" to the enum value Days.Tuesday. The Enum.Parse() method takes two parameters: the type of the enum (typeof(Days)) and the string representation of the enum value (userInput).
Remember to handle exceptions if the string does not match any of the enum values to prevent runtime errors.
Read more -
Convert int to enum in C#
How to combine two arrays without duplicate values in C#?
When to use Struct over Class in C#