Sunday, July 26, 2009

Extend your existing classes with extension method in 3.5

In 3.5 there is one good features called extension method now you can extend your functionality without modifying existing classes. Extension method allow developers to add own functionality to any existing classes. You don't need to create subclass or don't need to recompile existing classes and still you can extend that class with extension methods. Let's create an example to extend existing string classes to convert a simple string to bold html string.
public static class MyExtensions
public static string ConvertToBold(this string mystring)
System.Text.StringBuilder myBoldString =new System.Text.StringBuilder(string.Empty);
return myBoldString.ToString();  
So now our extension method is ready. Following the sample code to use this extension method.
protected void Page_Load(object sender, EventArgs e)
string helloWorld = "Hello World";
While running application you can out like below.


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews