BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 3.5+

Using Extension Methods with Enumerations

Extension methods were introduced in .NET 3.5 to allow classes to be apparently extended without modifying the class's code or using inheritance. Extension methods can also be used to add extra behaviour to enumerations.

Extension Methods and Enumerations

In this article we will examine two ways in which enumerations can be decorated with extension methods to add new functionality. In the first example we will create an enumerated type containing the names of vehicles that can be used for transportation. We will add an extension method to this enumeration that generates a string containing both the name and the value for the supplied vehicle type.

In the second example we will widen the scope of the extension method so that it works for any enumeration. The functionality of the extension method will be otherwise unchanged.

Creating an Extension Method for a Specific Enumeration

In many cases, extension methods for enumerations will be limited to a single enumerated type. We will demonstrate such an extension method in the first example. To begin, create a new console application and add the code shown below to define an enumeration that contains constants for five different forms of transport.

public enum VehicleType
{
    Bicycle,
    Car,
    Bus,
    Train,
    Aeroplane
}

All extension methods must be created within a static class, as a static method and with the type to be extended as the first parameter of the method. This first parameter must also be preceded by the "this" keyword. As we are extending the VehicleType enumeration, VehicleType will be the type for the first parameter. To create the extension method, add the following class to the project. This uses the string class's Format method to combine the value and name of the enumeration constant.

public static class EnumExtensions
{
    public static string ToText(this VehicleType type)
    {
        return string.Format("{0}:{1}", (int)type, type);
    }
}

We can now test the extension method. To do so, add the code below to the Main method of the Program class. This outputs the return value of the ToText extension method, when called for the Car constant.

Console.WriteLine(VehicleType.Car.ToText());    // Outputs "1:Car"

Creating an Extension Method for All Enumerations

You may wish to create an extension method that is available for all enumerations. This can be achieved in a similar manner to the previous example but does require some minor changes. The first modification is to the type of the initial parameter of the method. Instead of providing a specific enumerated type, the Enum class from the System namespace is used. This is the base class for all constants within enumerator lists. A common mistake is to try to use the enum keyword. However, "enum" and "Enum" are not interchangeable and using the lower case variation will cause a compiler error.

The second change is specific to our example extension method. In the previous code the value of the constant was determined by casting to int. As enumerations may use different sizes of integer as their underlying type, an Enum cannot be cast as a numeric type so this casting is no longer valid. Instead, we will convert to a 64-bit integer using the Convert class to ensure that all possible sizes are permissible.

Modify the extension method as shown below. You can then test the method by running the previous code and by trying different enumerations.

public static class EnumExtensions
{
    public static string ToText(this Enum constant)
    {
        return string.Format("{0}:{1}", Convert.ToInt64(constant), constant);
    }
}
18 June 2009