| LINQ-to-XML Examples... » |
Extension Method Example...
C# 3.0 added a cool feature to the toolkit called Extension Methods. Extension Methods are an extremely simple yet very powerful feature that allows you to bolt on additional method to existing objects without directly modifying those objects.
Follow up:
Have you ever wondered why methods appear to be missing from a class until you add 1 more additional using to your project, only to see the function show up with an (extension) prefix on the method in the code complete popup? Did you ever search a core .NET class for a method you can’t believe does not exist yet? Extension methods answer these questions and provide a more object-oriented, controlled way to extend third-party objects.
Here is a simple Extension Method example for future reference.
The System.Data.DataSet class has a function called GetXml() that returns a string containing the XML representation of the DataSet. I have found myself in the past wanting to get one tables XML representation. Sure, I could return the whole DataSet XML and parse out what I needed, but that is sloppy and wasteful. In the past, I created a static utility function like this…
public static string DataTableToXml(DataTable dt)
{
string sXml = string.Empty;
using (StringWriter sw = new StringWriter())
{
dt.WriteXml(sw);
sXml = sw.ToString();
}
return (sXml);
}
Let us assume, that I have a strongly typed DataSet with one table (would be more tables typically) that looks like this.

Calling the following function would return a string that looks like this (XML view of the string). Again, assuming we had loaded data into it.

Now let us do the Extension Method version. Since DataSet has a GetXml() function, I’ll create an extension method for DataTable called GetXml().
First, define the Extension Method
// static class to hold static extension methods
public static class DataTableExtensionMethods
{
// apply this extension to any datatable object.
public static string GetXml(this DataTable source)
{
if (source == null)
throw new ArgumentException("error");
string sXml = string.Empty;
using (StringWriter sw = new StringWriter())
{
source.WriteXml(sw);
sXml = sw.ToString();
}
return (sXml);
}
}
It is important to note that the extension method is static and must be wrapped inside a static class and the first parameter should contain the item you want to extend. Having extension methods inside a class allows for nice organization and grouping. You could have a static class for each grouping of extensions and group them into nice neat namespaces. To use the extension method add a using reference to your project and the GetXml() method will magically display in the code complete window with (extension) preceding it when you access a DataTable object.
So, loading my strongly typed dataset and calling the GetXml would look like this.
TestDataSet _ds = new TestDataSet();
_ds.Namespace = "";
_ds.DataSetName = "Customers";
_ds.Customer.AddCustomerRow("Ken", "Nuhfer");
_ds.Customer.AddCustomerRow("Chico", "Rodriquez");
string sXml = _ds.Customer.GetXml();
Calling this function returns the same XML string as above.
Extension Methods supply an extremely clean, safe and easy way to extend third-party objects when you don’t have access or it wouldn’t be advantageous to modify their source.