How to Convert String To Enum

In C#, Enum plays a very important role which making code more readable. In this article learn how to convert String to Enum.


How to Convert String To Enum | Convert String to Enum

This example shows how to convert enum values to a string and vice versa.

Color color = Color.Red;
string str = color.ToString();  // "Red"

String to enum:

using System;  
string str = "Red";
Color animal = (Color)Enum.Parse(typeof(Color), str);  // Color.Red
Color animal = (Color)Enum.Parse(typeof(Color), str, true); // case insensitive
 


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo