INTERNET APPLICATION DEVELOPMENT
MID MARKET ERP DEVELOPMENT
by Jacky Xu
Recently, I was researching .net linq syntax and found one interesting function: Extension Methods. I found myself asking, Why can linq add many methods to many existing classes? Did Microsoft change these classes? The answer is no. They are using a new c# 3.0 feature: Extension Methods. The old way is to inherit the “string” class and add it to your new method, and use your new method in your code. But, through the extension method, you can add new methods to existing types without creating derived types.
You can create a static method in your class and put keyword “this” type and variable as the first parameter in the method.
Before you add the extension class to your project, you need to find the methods for string type variable.
Now we can add the new class to your project, then you can find the new methods for string-type variables.
“Extension Methods” need to reference the namespace “System.Linq.” And, it cannot access the private method in the extended type. If you create an extension method that has the same signature method inside the type that you want to extend, the new extension method will never be called.
I’m looking forward to using this on a customization project soon.