4

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

*

Why do we put extension methods inside static class?

*

According to MSDN,

**Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

**

What is the role of this operator here and how does it associates that extension method to that argument?

F11
  • 3,703
  • 12
  • 49
  • 83

4 Answers4

7

No, you can't define an extension method on a class that is not static.

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

So basically:

var test = myInstance.MyExtensionMethod();

is the same as

var test = MyExtensionClass.MyExtensionMethod(myInstance);
ken2k
  • 48,145
  • 10
  • 116
  • 176
2

They are 4 requirements for method to be an extension method:

  • It has to be declared in static class
  • It has to be static (which actually is always true if the first one is met)
  • It has to be public
  • It has to have first parameter marked with this keyword

So you can't define extension method in non-static class.

Whole Extension Method functionality is some kind of syntax sugar. Following extension method declared on 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 be transformed into the first one by compiler anyway.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
2

What is the role of this operator here and how does it associates that extension method to that argument?

In this context this is not an operator, it is a modifier. It could have been called something else, it has no relation to this object which refers to the current object within a normal method call.

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

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
1

No, extension methods have to be in a static class, that's just the rule. It could have been possible to allow extension methods to be defined anywhere, but to make it easier to find them they are not allowed to be buried inside classes with a lot of other code.

The this keyword is used on the first parameter of an extension method to specify that it is an extension method.

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

Guffa
  • 687,336
  • 108
  • 737
  • 1,005