Sunday, June 27, 2010

.Net 3.5: Extension Methods

First time I am writing a blog that too with having a coffee, now the time is 3 o' clock in the morning but I am not going to sleep until I complete this blog.

OK.......Let us see what is Extension Methods.

What you do if you want to add a method to a class , yes it is simple, why not, we can add a method to a class, this is OK.

You want to add a method to a class but you don't have the source code of that class but still you want to add a method.

Without disturbing the source code of a class you want to add a method to that class.

Another case, You want to add a method which can be used by several different classes which are not from same family and you want to define the method in a central repository.

Extension Methods is the answer for all the above questions.

Extension methods enable you to add the behavior (method) externally to a class. You can define a method in a central repository and then you can attach that method to several different classes without disturbing the source code of that classes.

You can extend the existing functionality without changing the existing code. Extension methods is a great way to provide flexible design.

An extension method is nothing but Microsoft implementation of Visitor Pattern.

Visitor Pattern:


IVisitor is an interface, which is responsible for calling the exact implementaion (VisitorImp1A) of the methods. So if Class A wants to call a method then Class A will call the IVisitor and the IVisitor will decide the exact method to attach it to the Class A. VisitorImp1A can be attached to all the classes A, B and C.

Visitor Pattern : Allows you to define a new operations with out changing the classes on which it operates.


Extension method signature:
static string reverse(this string str, string value)

static: Extension method should be static method, static is a keyword which is used to declare a method as a static method.

string: return type of the reverse Extension method is a string.
reverse: reverse is the extension method name.

this: ‘this’ Keyword is used to denote that the current method is an extended method of this class.

the first parameter of a Extension method can be the name of the class to which this method is attached externally. Here the Extension method reverse is attached to the String class.

Create an ordinary method and the 1st parameter of the method can be the class using the keyword this to which the method is attached externally.

The class in which the extended methods are created that class should be a static class.

Sample Code:

In this sample code i am going to show you how to add a reverse method to a String class using Extension Methods.

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

namespace Extension_Methods
{
public static class StringExtension
{
//Adding Extension method called reverse to the String class.
//this Keyword denotes that the reverse method is an extended method of String class.
public static string reverse(this string str, string value)
{
Char[] chars = value.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
}
}

I created a class called StringExtension inside this class I created a method called reverse and added it to the String class by declaring in the first parameter of the method this string str, which tells that this reverse method is a extension method of the string class. Don't forget the keyword this because this makes the difference of your method from Ordinary method to an Extension method.

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

namespace Extension_Methods
{
class Program
{
static void Main(string[] args)
{
String reverseValue = "Welcome";
//Calling the Extension method called reverse.
reverseValue = reverseValue.reverse(reverseValue);
Console.WriteLine(reverseValue);
Console.ReadLine();
}
}
}

I am calling the reverse method that I created in the StringExtension class and added it to the String class and I am printing the reverse value of "Welcome" using the Extension method called reverse.

If you want to make some changes to the reverse method then you can do the changes in the StringExtension class without anthing to the String class.

Interesting right!

Thanks to Extension Methods. Ok guys, See you in my next blog.