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 value of an enum, cast it to an int. (By default, an enum is an int, though you can specify other numeric values for them.)

int i = (int)Level.Low;
Console.WriteLine(i);
/* output:
1
*/

If you have a string and want to convert it to an enum, use the Enum.Parse() method. Pass the enum type, the string value to parse, and an optional boolean parameter to note if you want the parsing to be case-insensitive. (By default, parsing is case-sensitive.)

Level lvl;

// case-insensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), "medium", true);
Console.WriteLine(i);
/* output:
Medium
*/

// default, case-sensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), "high");
/* throws an exception:
System.ArgumentException: Requested value 'high' was not found.
*/

If you have a numeric value and want to convert it to an enum, simply typecast the number to the enum type. Remember, an enum is just a number!

int val = 3;
Level lvl = (Level)val;
Console.WriteLine(lvl);
/* output:
High
*/

What about a set of enums that are not mutually exclusive (i.e. a sets of flags)? A “flags” enum can have multiple values, and is decorated with the [Flags] attribute, as shown below. Note that a these enums should have values defined in powers of two (1, 2, 4, 8, etc.). See below for a sporting example.

[Flags]
public enum Bases
{
	Empty = 0,
	First = 1,
	Second = 2,
	Third = 4,
	FirstAndSecond = Bases.First + Bases.Second,
	FirstAndThird = Bases.First + Bases.Third,
	SecondAndThird = Bases.Second + Bases.Third,
	Loaded = Bases.First + Bases.Second + Bases.Third
}

Many of the previously defined operations work on these enums. String output is as you would expect when using ToString():

Console.WriteLine(Bases.Empty.ToString());
Console.WriteLine(Bases.FirstAndSecond.ToString());
/* output:
Empty
FirstAndSecond
*/

Getting the numeric value is the same, too:

int i = (int)Bases.First;
int j = (int)Bases.SecondAndThird;
Console.WriteLine(i);
Console.WriteLine(j);
/* output:
1
6
*/

Parsing — yes, the same.

Bases b1 = (Bases)Enum.Parse(typeof(Bases), "second", true);
Bases b2 = (Bases)Enum.Parse(typeof(Bases), "Loaded");
Console.WriteLine(b1);
Console.WriteLine(b2);
/* output:
Second
Loaded
*/

Numeric values? The same as well.

int val = 5;
Bases bases = (Bases)val;
Console.WriteLine(bases);
/* output:
FirstAndThird
*/

One thing you’d want to do with a flagg enum is to detect if a particular flag is set. It’s easy to do using the AND operator (&).

bool isFirstOnSecondOrThird = (Bases.First & Bases.SecondAndThird) == Bases.First;
bool isFirstOnLoaded = (Bases.First & Bases.Loaded) == Bases.First;

/* output:
False
True
*/

There’s lots more that you can do with enums — check out some of the following links for more info:

C# 3.0’s syntatic sugar

In the old days, we’d write something like this:

view.Rows.Add("Type");
view.Rows.Add("Count");
view.Rows.Add("Whatever");

With C# 3.0 we can do this:

new string[] { "Type", "Count", "Whatever" }
    .ForEach(str => view.Rows.Add(str));

That is very cool syntatic sugar. Sure, this is not new news, but when you think about C# in the context of the new features available in 3.0, it really starts to feel a lot more like coding with JavaScript (which is a good thing).

Google AdSense ViewComponent for MonoRail

Like many, I use Google AdSense to host ads on my web sites. Including the ad code typically requires you to paste a block a block of JavaScript onto the page. I don’t mind injecting the JavaScript, but I wanted to come up with a better way.

The solution I came up with was a quick MonoRail ViewComponent, AdUnit. It has a simple purpose: to render a block of code for an ad unit of a given size.

The first task was to create an enum which represented all the possible ad unit sizes, based on what’s offered by Google. Continue reading