Extension methods are introduced in .NET Framework 3.5. These are the special kind of static methods. Extension methods are mainly useful when you want to extend an existing type like Integer, String e.t.c. You can implement any type using extension methods.
Now i will show you an example on extension methods. In this example i will show you how to extend the DateTime type to get custom date format.
1. Create a new console application.
2. Create a static class with name "ExtensionMethods"
public static class ExtensionMethods
{
}
3. Inside ExtensionMethods class create an extension method
-- Remember the first parameter of the extension method must be preceded with "this" keyword
public static string GetCustomDate(this DateTime value)
{
}
4. Now write the following statement to return the date in "MMM dd, yyyy" (Ex: Oct 30, 2013) format.
return value.ToString("MMM dd, yyyy");
5. Finally your extension class looks as follows.
public static class ExtensionMethods
{
public static string GetCustomDate(this DateTime value)
{
return value.ToString("MMM dd, yyyy");
}
}
6. Now go to Main method and call the DateTime extension method as follows
class Program
{
static void Main(string[] args)
{
string value = DateTime.Now.GetCustomDate(); // Called like an instance method.
Console.WriteLine(value);
}
}
7. Here the extension method will return the output in custom date time format like "Oct 30, 2013".
Complete Code
Now i will show you an example on extension methods. In this example i will show you how to extend the DateTime type to get custom date format.
1. Create a new console application.
2. Create a static class with name "ExtensionMethods"
public static class ExtensionMethods
{
}
3. Inside ExtensionMethods class create an extension method
-- Remember the first parameter of the extension method must be preceded with "this" keyword
public static string GetCustomDate(this DateTime value)
{
}
4. Now write the following statement to return the date in "MMM dd, yyyy" (Ex: Oct 30, 2013) format.
return value.ToString("MMM dd, yyyy");
5. Finally your extension class looks as follows.
public static class ExtensionMethods
{
public static string GetCustomDate(this DateTime value)
{
return value.ToString("MMM dd, yyyy");
}
}
6. Now go to Main method and call the DateTime extension method as follows
class Program
{
static void Main(string[] args)
{
string value = DateTime.Now.GetCustomDate(); // Called like an instance method.
Console.WriteLine(value);
}
}
7. Here the extension method will return the output in custom date time format like "Oct 30, 2013".
Complete Code
using System;
namespace
CustomExtensions
{
//Extension methods must be defined in static
class
public static
class
ExtensionMethods
{
// The first parameter of the extension method
//should be preceded with this keyword
public static
string GetCustomDate(this
DateTime value)
{
return value.ToString("MMM dd, yyyy");
}
}
class Program
{
static void
Main(string[] args)
{
//Pass the date time value to the extension
method
string value =
DateTime.Now.GetCustomDate();
// Extension method returns "Oct 30, 2013"
Console.WriteLine(value);
}
}
}