Click here to Skip to main content
15,881,567 members
Please Sign up or sign in to vote.
4.60/5 (6 votes)
Is extension method = static method returning strongly typed variables or collections?

i read one entry about it saying -- "this" keyword as a static method param tells The compiler that this particular Extension Method should be added to objects of type default .net class.

SO do we need to add "this" keyword for creating extended method or every static method in static class can be called extension method?
Posted

From here[^],
"Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type."

Member 3981366 wrote:
returning strongly typed variables or collections

Sorry, I don't understand what you mean.

Member 3981366 wrote:
do we need to add "this" keyword for creating extended method

Yes, if you want it to be used as an Extension method i.e. the compiler automatically glueing the code for linking the static extension method to the instance object

Member 3981366 wrote:
every static method in static class can be called extension method

No, since you cannnot use static methods with instance object. There lies the major inclination of extension methods.
 
Share this answer
 
Comments
RDBurmon 2-Nov-10 6:22am    
Hatsoff man .Proper style of giving answer.
Yes.

When you use static methods within static class and use "this" keyword then this method known as Extended methods.
 
Share this answer
 
To specify an extension method you insert the keyword this before the
first method parameter, which indicates to the compiler the type you
want to extend. Note that the method and its class must be static.
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900