Extension Methods allow you to add additional methods to an existing type without affecting the original's specification. It enables you to extend the functionality of a type that you may not be able to inherit (because it is sealed) or for which you do not have the source code.
Here's an illustration. To DateTime, we'll add an Age() method:
public class Program
{
public static void Main()
{
DateTime birthdate = new DateTime(1980, 10, 7);
Console.WriteLine("Your age is: " + DateTime.Now.Age(birthdate));
}
}
public static class Extensions
{
public static int Age(this DateTime date, DateTime birthDate)
{
int birthYear = birthDate.Year;
int currentYear = DateTime.Now.Year;
return currentYear - birthYear - 1;
}
}
We added a new Age() function to C#'s DateTime type, as you can see in the example. If you need more examples, reply back to me.