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!
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.
- Add a new static class called “Strings” to your project (e.g. “public static class Strings”).
- I removed the namespace so I can use these extended methods without having to import the namespace.
- Now add a method called “public static bool EndsWith(this String value, String values)”.
- I also created one that accepts a List<string> type as the second parameter as well.
- Then add the following code shown in “Figure 1” under Code below, to the method.
- Now go to a page where you want to use this new extended method.
- At the end of a string, place a “.EndsWith(STRINGARRAY)”.
- Of course STRINGARRAY is the string array containing the string items to determine if your value ends in.
Let’s look at what’s happening above.
- 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.
- 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.
- 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.
- Next we go to the page that we want to use this new extended method in.
- Finally we actually use the new method. Note that you don’t have to pass the first parameter, just the remaining ones.
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.