Sunday, January 29, 2012

C# 3.0 for Beginners - Extension Methods - Part 2


Custom Extension Methods

C# 3.0 enables you with the ability to extend the functionality of existing types. This means that you can write your own extension methods that would give the programmer the feeling that those are just methods provided by the existing type.

For example, System.Int32 doesn't provide you an IsEven() method which would tell you if the integer is holding an even value. It would obviously be great if you could write a method which does this, just in case you use this functionality heavily in your code. You would then write a method like this
public bool IsEven(Int32 i)
{
    if(i%2 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Well, that's perfectly fine. But, I wish we could make it more easier. And I bet we can for sure, that's what extension methods are here for. No, we cannot go and implement the IsEven() method for the Int32 type, but C# 3.0 allows you to extend type functionality in a different way.

Check out the following code. I'm extending the Int32 type to expose the Int32.IsEven() extension method.
public static class MyIntegerExtensionMethod
{
    public static bool IsEven(this Int32 i)
    {
        if (i % 2 == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Int32 n = 9;
        Console.WriteLine(n.IsEven());
    }
}

Let's look at the most important part, the method signature.
public static bool IsEven(this Int32 i)

A template for the above would be

<access_modifier> static <return_type> Method_Name(this <extended_type> <instance_of_type>, <args_list>)

While <access_modifer> and <return_type> are intuitive, special attention is needed at the parameters of the method. The first parameter resembles the type which is to be extended, preceded with the 'this' keyword. The arguments that follow are the actual arguments/parameters to the method.

Another thing to remember is that extension methods should be defined as static members in a static class. The only difference between a normal method and an extension method is that the first parameter of an extension is always prefixed with the 'this' keyword. The rest are normal parameters. Also, when using the extension method you should remember to keep the extension method within the scope of its usage or you could even include it using the 'using' directive.

Usage Restrictions
  • Extension methods can access only the public members of the type being extended.
  • If you define an extension method whose signature matches with the method already existing in the extended type, priority is given to the existing method and the new extension method is ignored.
Have fun with this great feature!

No comments:

Post a Comment