Enum.Parse Example Screenshot

There are times when you need to be able to get an Enum from a string. For example, you could be reading in values from an XML document.

Anyway, accomplishing this is the easiest ever: use the System.Enum.Parse method.

public enum Villain
{
 Voldemort,
 Saramon,
 Palpatine,
 MrShadow,
 SourceOfAllEvil,
 Work
}

public static void Main(string[] args)
{
 Villain silliest = (Villain)System.Enum.Parse(typeof(Villain), "SourceOfAllEvil");

 Console.WriteLine("Silliest: " + silliest);

 Console.ReadLine();
}