Fun with Extension Methods and Enums

Extension methods are one of my favorite features in C# 3.0. Tim Rayburn has already posted something about how we can use extension methods on interfaces to do some cool things, allowing us to sort of provide some implementation code to interfaces.

Yesterday I was implementing something in a project that involved an enum, and was wandering whether extension methods also apply to enums; and they do!

Here’s the scenario: I have a ComparisonOperator enum that I use to keep a list of comparison operators that may be used to create where clauses on the fly. This enum looks something like this:

    public enum ComparisonOperator
    {
        Like,
        Equals,
        NotEquals,
        LessThan,
        GreaterThan
    }

In places where I use this enum, I need to convert values to their SQL equivalent. For instance, Equals should translate to "=", LessThan should translate to "<", and so on.

In .NET 2.0 I have created a helper method somewhere with a switch statement that does the translation, since enums cannot have their own methods. In .NET 3.5, though, I can create an extension method like so:

    public static class EnumExtensions
    {
        public static string ToSql(this ComparisonOperator token)
        {
            string sqlEquivalent = String.Empty;
            switch (token)
            {
                case ComparisonOperator.Like:
                    sqlEquivalent = "LIKE";
                    break;
                case ComparisonOperator.Equals:
                    sqlEquivalent = "=";
                    break;
                case ComparisonOperator.NotEquals:
                    sqlEquivalent = "!=";
                    break;
                case ComparisonOperator.LessThan:
                    sqlEquivalent = "<";
                    break;
                case ComparisonOperator.GreaterThan:
                    sqlEquivalent = ">";
                    break;
            }
            return sqlEquivalent;
        }
    }

The ToSql method extends the ComparisonOperator enum, by providing the translation between the enum value and the SQL equivalent string. Now I can easily use my enum and call the extension method on it, like so:

            ComparisonOperator likeOperator = ComparisonOperator.Like;
            ComparisonOperator notEqualsOperator = ComparisonOperator.NotEquals;
            ComparisonOperator lessThanOperator = ComparisonOperator.LessThan;

            Console.WriteLine(likeOperator.ToSql());  // prints LIKE
            Console.WriteLine(notEqualsOperator.ToSql()); // prints !=
            Console.WriteLine(lessThanOperator.ToSql()); // prints <

Of course, this is something that not every developer would understand, because nobody expects an enum to have methods, but things like this will begin to show up as people get more used to the new features in .NET 3.5.

Advertisements

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: