I wrote an article about this topic a few months ago but I wanted to explain it a little further in this article by showing real-world scenarios on when and how to use this feature.
So what does Extension Method mean? According to MSDN:
"Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type."
Now that we have that settled lets look at a good scenario that depicts a great time when to use an Extension Method.
A few weeks ago i wrote this small class to demonstrate how to use the #ZipLibrary. In that demo i showed you that #ZipLibrary is missing a key feature which is the ability to loop thru all the files and directories contained in a folder and add each one of them to a zip file. Now, wouldn't it be nice if #ZipLibrary has that feature? With the aid of extension methods you can add that functionality to #ZipLib without even modifying it's source code. You don't believe me? Then let me show you how you can accomplish this.
My idea is to build a set of methods that would enhance the ZipFile object specifically the Add function. I'd like to extend this method and add serveral overloads to it like Add(FileInfo), Add(DirectoryInfo), Add(FileSystemInfo) and Add(FileSystemInfo[]).
Now lets begin extending this class. First we need to create a class called SharpZipLibExtensions that would contain all the extension methods we are going to build and add the necessary using statements to this class.

I have added the ICSharpCode.SharpZipLib.Zip namespace to my class because this namespace contains the ZipFile object that we are going to extend and the System.IO namespace because this namespace contain the IO specific classes that we are going to use inside our extension method.
Next, lets take the core function from my previous post into this class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace KeithRull.Extensions
{
public static class SharpZipLibExtensions
{
/// <summary>
/// Iterate thru all the filesysteminfo objects and add it to our zip file
/// </summary>
/// <param name="fileSystemInfosToZip">a collection of files/directores</param>
/// <param name="z">our existing ZipFile object</param>
private static void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
{
//check whether the objects are null
if (fileSystemInfosToZip != null && z != null)
{
//iterate thru all the filesystem info objects
foreach (FileSystemInfo fi in fileSystemInfosToZip)
{
//check if it is a directory
if (fi is DirectoryInfo)
{
DirectoryInfo di = (DirectoryInfo)fi;
//add the directory
z.AddDirectory(di.FullName);
//drill thru the directory to get all
//the files and folders inside it.
GetFilesToZip(di.GetFileSystemInfos(), z);
}
else
{
//add it
z.Add(fi.FullName);
}
}
}
}
}
}
Next, lets start building our extension methods that would call the GetFilesToZip function.

/// <summary>
/// Add a File to the ZipFile
/// </summary>
/// <param name="z">ZipFile object</param>
/// <param name="fileToZip">the fileinfo object to zip</param>
public static void Add(this ZipFile z, FileInfo fileToZip)
{
Add(z, (FileSystemInfo)fileToZip);
}
/// <summary>
/// Add a Directory to the ZipFile
/// </summary>
/// <param name="z">ZipFile object</param>
/// <param name="directoryToZip">the DirectoryInfo object to zip</param>
public static void Add(this ZipFile z, DirectoryInfo directoryToZip)
{
Add(z, (FileSystemInfo)directoryToZip);
}
/// <summary>
/// Add a File/Directory to the ZipFile
/// </summary>
/// <param name="z">ZipFile object</param>
/// <param name="fileSystemInfoToZip">the FileSystemInfo object to zip</param>
public static void Add(this ZipFile z, FileSystemInfo fileSystemInfoToZip)
{
Add(z, new FileSystemInfo[]
{
fileSystemInfoToZip
});
}
/// <summary>
/// Add an array of File/Directory to the ZipFile
/// </summary>
/// <param name="z">ZipFile object</param>
/// <param name="fileSystemInfoToZip">the FileSystemInfo object to zip</param>
public static void Add(this ZipFile z, FileSystemInfo[] fileSystemInfosToZip)
{
GetFilesToZip(fileSystemInfosToZip, z);
}
One thing that you would quicly notice in the functions is the inclusion of a this keyword in the function parameter. The this keyword tells the compiler that the Add() function is an extension of the ZipFile object.
Now all you need to do is import the KeithRull.Extensions namespace to any class and every ICSharpCode.SharpZipLib.ZipFile object in that class would have the extension methods that we added.

That's how you use extension methods in C#.
Download the source for this article here: KeithRull.WorkingWithExtensionMethods.zip (120.39 KB)