Rounding-Out A Class Library – Extension Methods

November 1, 2010 by Ben Galluzzo    .NET |    Comments (0)

One potential improvement to a custom class library could lie in the use of extension methods.  In short, extension methods are a quick and simple way to extend a type without modifying or recompiling the actual type which is being extended.  Utilizing an extension method is as simple as calling the ToString() method on a type.  At first it may seem difficult to come up with uses for extension methods.  However, working through a project, potential use for an extension method may present itself and also could very well reside in commonly created utility classes.  Operations often performed against a type in the same context is a perfect use for converting a utility function into an extension method.

Defining an extension method might seem a little odd at first to the uninitiated.  Take a bit of time to think about what’s happening under the hood to provide this added functionality, automatically, to an existing type. The first parameter of an extension method takes on the this modifier to accept the type on which the method operates.  Utilizing the extension methods requires adding a using directive of the namespace for which the extension method resides. 

Below are a couple very simple examples of extension methods and their use.  These two extension methods provide a boolean value for a check of a null value or an empty value against an Array type and also a for String type.

using System;

namespace ProvingGrounds.Lib
{
    public static class LibraryExtensions
    {
        public static bool IsNullOrEmpty(this Array _value)
        {
            return (_value == null || _value.Length == 0);
        }

        public static bool IsNullOrEmpty(this string _value)
        {
            return (_value == null || _value.Length == 0);
        }

    }
}
Utilizing these new extension methods can only be done if applying the proper using directive to the calling class; in this case, using ProvingGrounds.Lib.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProvingGrounds.Lib;

namespace ProvingGrounds.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            string emptyTest = null;
            if (emptyTest.IsNullOrEmpty())
                System.Console.WriteLine("emptyTest is null or empty");
        }
    }
}

Omitting the proper using directive will result in the extension method not being available for use.

ExtensionMethod_271x101

 

 

 

 

Care should be taken when enabling more global usage. For instance, if the class housing the extension methods are added to the System namespace, their use can be made more readily available.

using System;

namespace System
{
    public static class SystemExtensions
    {
        public static bool IsNullOrEmpty(this Array _value)
        {
            return (_value == null || _value.Length == 0);
        }

        public static bool IsNullOrEmpty(this string _value)
        {
            return (_value == null || _value.Length == 0);
        }

    }
}

So there it is. A couple of very simply extension methods in practice. Have fun with them. Use them to simplify often used operations on types; operations whose functionality may already reside in common util classes.

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading







SQL Saturday - 506 - Baltimore BI Edition

Month List