.NET 4 Baby Steps: Part III - Enum

Photo of baby Note: This post is part of a series, and you can find the rest of the parts in the series index.

Enumerations are something I have posted about before because they are very useful—and with .NET 4, they have had two new methods added that could have been ripped from my personal feature requests:

HasFlag

Enums can be bitwise values if the [Flags] attribute is set, however checking the bitwise values was ugly and very confusing for people who hadn’t seen it before. For example:

[Flags]
enum CupboardContents
{
    Nothing = 0,
    Cups = 1,
    Plates = 2,
    Cuttlery = 4,
    Food = 8,
    DeadBodies = 16,
    Everything = Nothing | Cups | Plates | Cuttlery | Food | DeadBodies
}

static void Main(string[] args)
{
    CupboardContents motherHubberbs = CupboardContents.Nothing;
    CupboardContents normal = CupboardContents.Plates | CupboardContents.Cups | CupboardContents.Cuttlery;
    CupboardContents hitman = CupboardContents.DeadBodies;
    CupboardContents bursting = CupboardContents.Everything;

    CupboardContents test = CupboardContents.Plates;

    Console.WriteLine("Does {0} contain {1} = {2}", motherHubberbs, test, (motherHubberbs & test) == test);
    Console.WriteLine("Does {0} contain {1} = {2}", normal, test, (normal & test) == test);
    Console.WriteLine("Does {0} contain {1} = {2}", hitman, test, (hitman & test) == test);
    Console.WriteLine("Does {0} contain {1} = {2}", bursting, test, (bursting & test) == test);
}

This produces image

With .NET 4, we now have a simple method to do this check: HasFlag, which takes an enum and tells you if that flag is set. The following code does the same as the code above:

Console.WriteLine("Does {0} contain {1} = {2}", motherHubberbs, test, motherHubberbs.HasFlag(test));
Console.WriteLine("Does {0} contain {1} = {2}", normal, test, normal.HasFlag(test));
Console.WriteLine("Does {0} contain {1} = {2}", hitman, test, hitman.HasFlag(test));
Console.WriteLine("Does {0} contain {1} = {2}", bursting, test, bursting.HasFlag(test));

TryParse

Until now, the only way to get from a string to an enum was the Parse method:

enum Colours
{
    Red,
    Green,
    Blue
}

static void Main(string[] args)
{
    Colours red = (Colours)Enum.Parse(typeof(Colours), "Red");

    Console.WriteLine(red);
}

Which produces image

However, get the string wrong, and you were forced to deal with an ArgumentException:

image

With .NET 4, you now can use the TryParse method—and check the returned value first to see if the conversion was successful:

enum Colours
{
    Red,
    Green,
    Blue
}

static void Main(string[] args)
{
    Colours red;
    Colours purple;
    if (Enum.TryParse("Red", out red))
    {
        Console.WriteLine(red);
    }
    else
    {
        Console.WriteLine("Could not translate to a valid enum");
    }

    if (Enum.TryParse("Purple", out purple))
    {
        Console.WriteLine(purple);
    }
    else
    {
        Console.WriteLine("Could not translate to a valid enum");
    }
}

Which produces: image

Another benefit of this method over Parse is that it works with generics, meaning you do not need to pass the Type parameter in and cast the return value—as you had to do with Parse!