Extension methods let developers add methods to an existing type without having to recompile. Extension methods are static methods that can be used as instance methods of the type that was extended. Consider an example of a helper method that is used to format a phone number. Given a number in the format 999999999, the method returns a formatted string that looks like (999) 999-9999. Prior to C# 3.0, we would write a static helper method that takes the phone number string as a parameter and returns the formatted number.

Helper method for formatting a string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethods
{
    public static class StringHelper
    {
        public static string FormatPhoneNumber(string s)
        {
            return "(" + s.Substring(0, 3) + ")" + " " + s.Substring(3, 3) + "-" + s.Substring(6, 4);
        }
    }
}

We can call the static method by

class Program
{
    static void Main(string[] args)
    {
        string phoneNumber = "9037841234";
        Console.WriteLine(StringHelper.FormatPhoneNumber(phoneNumber));
    }
}

Let’s rewrite the method using Extension Methods feature. The best practice is to create the extension classes by suffixing the class name with the word Extensions. Let’s add a folder to the solution, call it Extensions and add a StringExtensions.cs static class.

image

Let’s take a look at the StringExtensions class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethods
{
    public static class StringExtensions
    {
        public static string FormatPhoneNumber(this string s)
        {
            return "(" + s.Substring(0, 3) + ")" + " " + s.Substring(3, 3) + "-" + s.Substring(6, 4);
        }
    }
}

At first glance, this looks very similar to the earlier listing of the helper method. Infact, the only difference is the “this” keyword. This is the magic keyword that tells the compiler to extend the string type. We can now use this extension method as if it was an instance method of the string type.

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            string phoneNumber = "9037841234";

            Console.WriteLine(phoneNumber.FormatPhoneNumber());
        }
    }
}

After we type the word phoneNumber and press the period, the intellisense in Visual Studio indicates that there are extension methods available and these are denoted by a little blue arrow pointing downwards as shown in the figure.

image

Why do we have to use Extension Methods when we can use static helper methods to achieve the same result? Extension methods produce code that is easier to read. Also, it’s more intuitive when you are writing code. If you are using static methods, you will need to know the name of the static class (StringHelper class in the above example) before the intellisense helps you with the method name. Extension methods come real handy when you have classes that you cannot extend. With extension methods, you can add functionality to some of the most basic classes in the .NET framework.

Retweetkick it on DotNetKicks.com