Enums in C#

An enum is a defined set of constants in a list. If you don’t give the enums any values they will start with 0 and count up. In this example we’ve forced the count up to start with 1.

public enum SingleColors { RED = 1, GREEN, BLUE, YELLOW }

You can change the type of the enum to any of the following (byte, sbyte, short, ushort, int, uint, long or ulong) like in this example:

public enum SingleColors : short { RED = 1, GREEN, BLUE, YELLOW }

Example: Fill a combo box (WPF) with all enum options.

comboBox1.ItemsSource = Enum.GetNames(typeof(SingleColors));

Example: How to convert a string to enum

SingleColors myColor = (SingleColors)Enum.Parse(typeof(SingleColors), "RED");

Example: Two different ways to convert an enum to string

string sMyColor = Enum.GetName(typeof(SingleColors), myColor);
sMyColor = ((SingleColors)myColor).ToString();