Friday, May 2, 2008

Extension methods with C# 3.5

Here is an example of Extension methods using C# 3.5

In this example we will put our own function in Frameworks string data type.

This will help user to print the the content of the stirng to console.





using System;
namespace ConsoleApplication1
{
public static class ExtendString
{
public static void PrintToConsole(this string str)
{
Console.WriteLine(str);
}
}
class Program
{
static void Main(string[] args)
{
string s = "Naveen.Prabhu";
s.PrintToConsole();
Console.Read();
}
}
}


Hope this will help you to understand the use of extension methods.

Create your own samples and take maximum use of this feature. :-)

No comments: