2010-01-06

Adding methods to existing types with Extension Methods

Ever felt like one of the types you work with could have used a couple of extra methods? Maybe you’d want to add to the string type a method called ToHyperLink, which will add HTML tags to your string to make it a valid HyperLink. In other words, if your string is

http://msdn.microsoft.com

and you call the method, you'll get something like

<a href='http://msdn.microsoft.com'>http://msdn.microsoft.com</a>

We basically have two approaches here: one, create a class that inherits string, and implement the method there. This will mean that if you wanted to use the method on an object, it'll have to be declared as your new type, not string. The second approach is to use Extension Methods.  With this technique, you get the possibility to "add" methods to existing types without having to create derived classes, thus extending their functionality.

Extension method have to be implemented inside a static class, and of course have to be static themselves. They could return any type you want, and have to have at least one parameter, of the type you want to extend, preceded by the word this. Check out this example:

  1: public static class Extensions
  2: {
  3:     public static string ToHyperLink(this string url)
  4:     {
  5:         string hl = string.Format("<a href='{0}'>{0}</a>", url);
  6:         return hl;
  7:     }
  8: }

In the above code, we created an extension method for the string type called ToHyperLink, which returns another string. It's that simple. As said before, the catch here is just to add the keyword this, and to make both the method and its class static.

Now, if we wanted to use it, we jus need a using reference to whatever the classes' namespace is, and call it just like we call any of the other string methods:

  1: //Here call the extension method an a variable
  2: string a = "http://msdn.microsoft.com";
  3: a = a.ToHyperLink();
  4: //Here we call the extension method on a plain string
  5: string b = "http://hugonne.blogspot.com".ToHyperLink();

Notice line 5? You can call the extension method directly on a string value rather than a variable. This is something we couldn't have achieved with inheritance.

Extension methods can also take additional parameters. Let's say we want to create an overload for the method, which takes the text for the HyperLink. We'd add a method to our class like this:

  1: public static string ToHyperLink(this string url, string linkText)
  2: {
  3:     string hl = string.Format("<a href='{0}'>{1}</a>", url, linkText);
  4:     return hl;
  5: }

Calling the method is just as simple:

  1: string c = "http://msdn.microsoft.com";
  2: c = c.ToHyperLink("Go to MSDN");
So, extension methods are are quick way to expand the functionality of our existing types. Nevertheless, it's always good to say you should try and use them only for simple operations. If you require some sort of more complex operations, it'll be always better to use inheritance, and make your own specialized types.

One final thing: extension methods only work in the .NET Framework 3.5.

No hay comentarios:

Publicar un comentario

Your tips and thoughts are always welcome, and they provide good motivation: