Click here to Skip to main content
15,891,316 members
Articles / All Topics

Extension Method

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
13 Feb 2010CPOL1 min read 13.6K   3   2
Power of Extension Method in Visual Studio
For the past few days, I was thinking about how Microsoft added a new method in existing classes of Visual Studio. I was surprised because I was creating List<> and I was showing me some LINQ methods, but when I commented “Using System.Linq” line, it was showing me default list methods.

But when comment “Using System.Linq”  was removed, Visual Studio was showing me some Linq methods in the intelligence window.

Then I started searching on the web for information about it. I found a new exciting feature of framework 3.0, which is “Extension Methods”. You can extend existing classes without inheritance using extension methods. There are some things which you should keep in mind while writing extension methods.
  1. You cannot override an existing method by writing an extension method.
  2. If an extension method has the same name as instance method, then it will not be called, because the priority of instance method is high. At compile time, extension method has low priority.
  3. You can write an extension for Properties, data Members and events.
It is very simple to write an extension method. Create a static class and write static extension method in this class. The “this” keyword in the parameters tells the compiler to add the extension method in the type after “this” keyword. In my example, “this string” compiler adds extension method to the string class.
C#
namespace ExtensionTest
{
 public static class ExtensionOfString
 {
   public static bool IsNumber(this string value)
   {
       Regex expression = new Regex(@"^[0-9]([\.\,][0-9]*)?$");
       return expression.IsMatch(value);
   }
 }
}
Here is an example to use extension method. Include the namespace and call your extension method like the original method of the instance.


C#
 using ExtensionTest;
. . . . .

string number = "4";
bool isNumber = number.IsNumber();
You can see how easy it is to write an extension method in .NET.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions

 
GeneralMy vote of 2 Pin
NilsKodeApe9-Feb-10 4:23
NilsKodeApe9-Feb-10 4:23 
GeneralRe: My vote of 2 Pin
EngleA12-Feb-10 4:00
EngleA12-Feb-10 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.