User Id :    Password :      New Member   Forgot Password  
Extend your existing classes with extension method in 3.5
Description Extend your existing classes with extension method in 3.5   No. of Views     1371
  Rating     0
Author Jalpesh Vadgama   Posted On     24 Jan 2012
Tags C#,C# : How To    

Sample Code   Download Code


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);
myBoldString.Append(""); myBoldString.Append(mystring); myBoldString.Append("");
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.



About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at Jalpesh Vadgama
No Photo
Country India
Company N/A
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.
Verification Code