How the extension method works

I want to understand how the extension method works? Can we define extension methods in non-stationary classes?

*

Why do we put extension methods inside a static class?

*

According to MSDN,

** Their first parameter indicates which type the method works for, and this modifier precedes this parameter. Extension methods are only available if you explicitly import the namespace into your source code using the using directive.

**

What is the role of this operator here and how does it associate this extension method with this argument?

+4
source share
4 answers

No, you cannot define an extension method for a non-static class.

this is syntactic sugar that allows you to call your static extension method on an instance. But at the end of the day, the extension method is nothing more than a static method in a static class.

So basically:

 var test = myInstance.MyExtensionMethod(); 

coincides with

 var test = MyExtensionClass.MyExtensionMethod(myInstance); 
+6
source

They represent 4 method requirements as an extension method:

  • It must be declared in a static class.
  • It should be static (which is actually always true if the first one is executed)
  • It must be publicly available.
  • It should have the first parameter marked with this keyword

Thus, you cannot define an extension method in a non-stationary class.

The functionality of the entire extension method is a kind of syntactic sugar. The following extension method declared in MyClass :

 // The following extension methods can be accessed by instances of any // class that is or inherits MyClass. public static class Extension { public static void MethodA(this MyClass myInterface, int i) { Console.WriteLine ("Extension.MethodA(this IMyInterface myInterface, int i)"); } } 

can be called in two ways:

 var myClassObject = new MyClass(); Extension.MethodA(myClassObject); 

or

 myClassObject.MethodA(); 

However, the second one will in any case be converted to the first one by the compiler.

+2
source

What is the role of the this operator here and how does it bind this extension method for this argument?

In this context, this not an operator; it is a modifier. It could be called something else; it is not related to the this object, which refers to the current object in a regular method call.

The role of the this modifier is to tell the compiler that it is actually an extension method and not a standard static method, so that it will not complain when you call it so that it looks like an instance method call, although it is not.

+2
source

No, extension methods should be in a static class, this is just a rule. It might have been possible to allow extension methods anywhere, but to make them easier to find, they cannot be buried inside classes with many other codes.

The this used in the first parameter of the extension method to indicate that it is an extension method.

(The internal implementation of the regular method also has a reference to the object as the first parameter, so the compiler with extension methods simply adds them to other methods in the class.)

+1
source

All Articles