Create enum instance from a string in CSharp
Description
The following code shows how to create enum instance from a string.
Example
using System;//from www . ja v a 2s. c o m
enum Color
{
red,
green,
yellow
}
public class MainClass
{
public static void Main()
{
Color c = Color.red;
// enum value from a string, ignore case
c = (Color) Enum.Parse(typeof(Color), "Red", true);
Console.WriteLine("string value is: {0}", c);
}
}
The code above generates the following result.