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.

Search This Blog
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews