Monday, January 25, 2010

How to add your own method/function to a .NET Framework classes

This is also called as a extension. Imagine that you are working with String Class, In there think that you want to add a your own method to the String class. So that is a extension and in .NET 3.0 introduced a way to do this using this key word.

It is soo…  easy. you have to create a static class with a public modifier and write you method by using this keyword in front of the parameter.

public static class MyExtensions
public static String getGreeting(this String name)
return "Hi !" + name;

Now you can see Visual Studio shows the method for you when you use the String class.


No comments: