If you have been programming in C# for a little while then you probably already have seen a couple of extension methods by now. What I did notice however was that there are clearly 2 different kind of developers making those extension methods. It looks like that there are developers who strive to make the best looking and most useful and well written extension methods and the other group of developers is trying to make an extension method out of everything. In order to be able to say what is good practice and what is bad, we are going to look at a couple of extension methods.

The first extension method that we are going to look at is compact and easy to use. At the other hand It’s also not so difficult to just write out what it does in your code. Here is the extension method:

public static bool IsBetween<T>(this T value, T low, T high) where T : IComparable<T>
{
    return value.CompareTo(low) >= 0 &amp;&amp; value.CompareTo(high) <= 0;
}

In our code we are now able to use this extension method like this:

if(user.Age.IsBetween(12, 16)) { ..

instead of what we would usulay do:

if(user.Age > = 12 &amp;&amp; user.Age <= 16) { ..

In this case I do think that the extension method does make the code a little better to read. Another plus is that the extension will only show up for something that implements the IComparable interface so that we will not get intellisence ‘pollution’.

Here is another extension method with a little more code:

public static string RemoveNonNumeric(this string s)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.Length; i++)
    if (Char.IsNumber(s[i]))
        sb.Append(s[i]);
    return sb.ToString();
}

And you can use this extension method like this:

"(020) 555-555-123".RemoveNonNumeric();

I think this one falls in the category ‘sure, use it’. For me it’s as clear as using a trim function.

Ok, here are the last two extension methods for where I think there is little discussion over. These 2 extension methods will help you converting strings or numbers back to an enum. Here is the code:

        public static T ToEnum<T>(this int number)
        {
            return (T)Enum.ToObject(typeof(T), number);
        }
        public static T ToEnum<T>(this string text)
        {
            return (T)Enum.Parse(default(T).GetType(), text);
        }

And these extension methods can be used like this:

MyStatusEnum status = 1.ToEnum<MyStatusEnum>();
status = "Good".ToEnum<MyStatusEnum>();

You do will see the ToEnum extension method in the intelisence of every string and integer, but you do have to specify the enum type that you want to cast it to.

Here it comes. We are now going into the grey area. Below you can see a series of extension methods that will let you use every String.Format combination as a FormatWith extension method. These extension methods are used in more than one project that I have worked on and there have been various discussions about if these were good or bad. I think both sides are partially right. Here is the code:

public static string FormatWith(this string format, object arg0)
{
    return string.Format(CultureInfo.InvariantCulture, format, arg0);
}
public static string FormatWith(this string format, object arg0, object arg1)
{
    return string.Format(CultureInfo.InvariantCulture, format, arg0, arg1);
}
public static string FormatWith(this string format, object arg0, object arg1, object arg2)
{
    return string.Format(CultureInfo.InvariantCulture, format, arg0, arg1, arg2);
}
public static string FormatWith(this string format, params object[] args)
{
    return string.Format(CultureInfo.InvariantCulture, format, args);
}
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
    return string.Format(provider, format, args);
}

With these extension methods you are now able to write code like:

"Hello {0}, my name is {1}".FormatWith("you","Edwin");

Instead of the usual:

String.Format("Hello {0}, my name is {1}", "you","Edwin");

When I first looked at these extension methods I too thought I could as well just use the existing String.Format method. And then it also has the disadvantage that you will lose the compile time check for the number of parameters in your call. But then I noticed that during coding the.FormatWith often feels more natural to use. By now I am using the .FormatWith more and more.

The previous extension methods are a little dubious but I think the next couple of methods are more wrong than right. At first these extension methods look handy but then when you start using them you will see that they could be confusing. Here is the code:

public static int MilisecondsToSeconds(this int num)
{
   return num / 1000;
}
public static int SecondsToMiliseconds(this int num)
{
   return num * 1000;
}
public static int MinutesToMiliseconds(this int num)
{
   return num * 60000;
}

You can then start using these extension methods like this:

Thread.Sleep(5.SecondsToMiliseconds());
proxy.Timeout = 1.MinutesToMiliseconds();

At first this code looks ok to use, however it is also possible to use code like this:

int minutes = 5;
Thread Sleep(minutes.SecondsToMiliseconds());

With this code the mistake is obvious , but when you start using variables without ‘minutes’ in its name, this mistake is easier to make. One other thing is that intelisence will show you the SecondsToMiliseconds method for every integer there is. I think that if for most values an extension method does not apply, then you should not have that extension method. Instead you could use a class with a static method like this:

Thread.Sleep (Convert. SecondsToMiliseconds (5));

Here is another simple extension method that falls in the same category:

        public static Uri ToUri(this String uri)
        {
            return new Uri(uri);
        }

You can use this extension method like this:

            Uri myUri = "http://www.evict.nl".ToUri();

With this extension method intelisence will let you see the ToUri on every string.

Here is another nice example of an extension method that looks very cool and easy to use but should actually be avoided. Here is the code:

public static Boolean And(this Boolean b,  Boolean  expression)
{
    return b &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; expression;
}
public static Boolean Or(this Boolean b, Boolean expression)
{
    return b || expression;
}

You can then use these extension methods like this:

string myVal = ...
if(String.IsNullOrEmpty(myVal).Or(myVal=="N/A")) { ..

This extension method does not make your code more readable. Instead you could as wel use this code:

if(String.IsNullOrEmpty(myVal)  || myVal=="N/A") { ..

I don’t see any benefit for using this extension method. Instead you will just add some extra complexity to your project.

I do like to use extension methods. Especially when I can use it using generic types like the enum conversion methods at the beginning of this article.