Extended Methods in .NET


Overview

Have you ever run across new functionality in .NET and wonder how you ever lived without it before? That’s what I think about Extended Methods. These are static methods that act as if they were instance methods on the extended type. They allow you to add methods to existing types without having to create new derived types, recompile or modify the existing type and they’re incredibly easy to implement.

This can already be witnessed in the System.Linq namespace. Create a new type that implements IEnumerable or Generic.IEnumerable<T> and then view the methods available to it. Now, put in a System.Linq namespace directive (using System.Linq;) and view the methods availabe to the IEnumerable. You all of a sudden have methods like GroupBy, OrderBy, Average, etc. available to you. No recompiling was necessary. You simply imported a new namespace. This is exciting!

Example

Let’s look at a real-world sample. I love having the String.EndsWith method available, but if I want to test whether the string ends in multiple values, I have to chain a bunch of String.EndsWith(“Value1”) || String.EndsWith(“Value2”) and so on, together. Instead, I’d like something similar to MS SQL’s “IN” command. So, let’s extend the String’s EndsWith method.

Extending String.EndsWith

  1. Add a new static class called “Strings” to your project (e.g. “public static class Strings”).
    1. I removed the namespace so I can use these extended methods without having to import the namespace.
  2. Now add a method called “public static bool EndsWith(this String value, String[] values)”.
    1. I also created one that accepts a List<string> type as the second parameter as well.
  3. Then add the following code shown in “Figure 1” under Code below, to the method.
  4. Now go to a page where you want to use this new extended method.
  5. At the end of a string, place a “.EndsWith(STRINGARRAY)”.
    1. Of course STRINGARRAY is the string array containing the string items to determine if your value ends in.

Explanation

Let’s look at what’s happening above.

  1. First we create a new class to put our extended methods in. It must be a static class, but will be used as if it’s an instance member of a class.
  2. Next We add a new method that will “extend” the String class to contain a new overloaded version of EndsWith. Note that the first parameter contains a “this” keyword. That tells the compiler that the first parameter will be the string value you put the “.EndsWith” at the end of. You will only need to pass the second parameter since the first is passed in automatically.
  3. Then we add the code that’ll actually go through each string value in the array to determine if the string being tested ends in any of them.
  4. Next we go to the page that we want to use this new extended method in.
  5. Finally we actually use the new method. Note that you don’t have to pass the first parameter, just the remaining ones.

Summary

In summary, extended methods are incredibly useful for adding functionality that .NET doesn’t come stock with. So far I’ve added methods to automatically turn a string array or generic string list into an HTML table row, automatic conversion from string array or string lists to delimited strings, ToHtmlBold/Italic, String Filtering and many more methods. These quickly and easily allow all strings throughout my application to implement this functionality without having to reinvent the wheel in every class where I use particular methods.

I’ve also seen this used by people to automatically add information to a URL (“Response.Redirect(“URL”.AddSessionInfo, false);”) and a whole lot of other things. The application of this is massive and really reduces the overhead of developers that perform common tasks repeatedly across many pages.

I hope this helps you as much as it has me! You can also view this link to Microsoft’s MSDN page for Extended Methods.

http://msdn.microsoft.com/en-us/library/bb383977(v=VS.100).aspx

Code

EndsWith Extended Method
Figure 1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s