get enum by string c# code example
Example 1: c# get enum value from string
Keys key = (Keys)Enum.Parse(typeof(Keys), "Space");
Example 2: how to pass string value to enum in c#
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myString);
Example 3: c# get string value of enum
using System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
}
}