Posts Tagged C#

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.

Leave a Comment

C#3.0 WebCast for the VBUG – Material

Just finished the presentation. It went pretty well, I think (or so was I told).  :)

The slide deck and source code can be download here.

And for those of you waiting on my next posts on productivity, it is coming! I started writing my next post, but haven’t finished yet. I should have it out on the next few days.

Leave a Comment

Follow

Get every new post delivered to your Inbox.