Monthly Archives July 2009

Everything you ever wanted to know about an enum (well, almost everything)

The enum, what a lovely creature available to us in C#. They help make our code more readable and bug-resistant. They also have some tricks up their sleeves.
Take a sample enum declaration:

public enum Level
{
Unknown = 0,
Low,
Medium,
High
}

If you want a string representation of an enum, you simply use the ToString() method.

Console.WriteLine(Level.Unknown.ToString());
/* output:
Unknown
*/

If you want the numeric [...]