1enum Colour
2{
3 Red,
4 Green,
5 Blue
6}
7
8// ...
9Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
10Console.WriteLine("Colour Value: {0}", c.ToString());
11
12// Picking an invalid colour throws an ArgumentException. To
13// avoid this, call Enum.IsDefined() first, as follows:
14string nonColour = "Polkadot";
15
16if (Enum.IsDefined(typeof(Colour), nonColour))
17 c = (Colour) Enum.Parse(typeof(Colour), nonColour, true);
18else
19 MessageBox.Show("Uh oh!");