One of the new features in the .NET framework is extension methods. Extension methods allow you to extend existing types by adding new methods without using inheritance. Their implementation is trivial and, with a little creativity, it's very easy to find ways in which extension methods can add new functionality to existing types. Ever wish a variable could have an "IsNull" method? It's quite easy to add something like this using extension methods.
To create an extension method you create a static class with a static method that takes as it's parameter the type you wish to extend, which you can see in the following example:
NOTE: In the following example I extend several different types within the same class\namespace. I think it's probably advisable to avoid doing this and group your items appropriately.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ExtensionMethods
{
public static class MyExtensionMethods
{
/// <summary>
/// Indicates whether the specified <see cref="T:System.Data.DataTable"></see> object is null or empty.
/// </summary>
/// <param name="dt">The DataTable to be evaluated</param>
/// <returns>True if the DataTable is null or contains no rows, otherwise false.</returns>
public static bool IsNullOrEmpty(this DataTable dt)
{
if ((dt == null) || (dt.Rows.Count < 1))
{
return true;
}
return false;
}
/// <summary>
/// Indicates whether the specified <see cref=""T:System.Object"></see> is null.
/// </summary>
/// <param name="obj">The object to be evaluated.</param>
/// <returns>True if the object is null, otherwise false.</returns>
public static bool IsNull(this object obj)
{
return (obj == null);
}
/// <summary>Indicates whether the specified <see cref="T:System.String"></see> object is null or an <see cref="F:System.String.Empty"></see> string.</summary>
/// <returns>true if the value parameter is null or an empty string (""); otherwise, false.</returns>
/// <param name="value">A <see cref="T:System.String"></see> reference. </param>
public static bool IsNullOrEmpty(this string s)
{
return (string.IsNullOrEmpty(s));
}
}
}
In this class I've extended three types; DataTable, Object, and String. I can think of some much better uses, such as an "IsValidEmailAddress" method to extend String, or one that I've seen posted by others "IsRegExMatch". I chose these particular examples because I feel they help convey the ease in which extension methods can be created to add missing functionality or to simply make existing functionality accessible in a different way. I realize the methods in the example are not very useful or creative, but the point is simply to demonstrate what can be done.
To make use of the extension methods you've created you need to do nothing more than add a "using" to import the namespace containing your extention methods. For example:
using ExtensionMethods;
Once you've added the "using", all of the extension methods within that namespace are now available to use:
If you haven't looked at extension methods before you may be curious if an "IsNull" extension method will work on a null object and the answer is yes, it does.
As much as I can appreciate extension methods, I can see where some may prefer to have a separate Utility class. Perhaps they find this approach easier to understand. maintain, or discover. Personally, I believe they should be used sparingly and with a careful eye to avoid creating code discovery problems. Also there's some debate as to whether they should be used at all on classes you control, as opposed to intrinsic framework types or those found in third party libraries. Certainly there is the potential to use them irresponsibly, as is the case with many things within the framework, so just use good judgement. As with anything, I'm sure extension methods will have their share of both lover's and hater's. What's your take?