-2

In C#, the "this" reference is implicitly used when invoking methods of a class. The same does not seem to hold when using extension methods:

public static class TestExtensions
{
    public static void ExtensionMethod(this Test t)
    {
        // IMPLEMENTATION
    }
}

public class Test 
{
    public void A()
    {
        B(); // implicitly this.B();
    }

    public void B()
    {
        ExtensionMethod(); // doesn't work!
        this.ExtensionMethod(); // works!
    }
}

I understand that extension methods are merely "sugar" that passes the "this" reference into the statis extension method. But why should it enforce also adding the "this" keyword, if it can find the method to invoke anyway ?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • 1
    http://stackoverflow.com/questions/3510964/why-is-the-this-keyword-required-to-call-an-extension-method-from-within-the-e – Farhad Jabiyev May 13 '15 at 08:28
  • @FarhadJabiyev: That is the duplicate question indeed. As you see on top of the question. – Patrick Hofman May 13 '15 at 08:28
  • `Extension methods` are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the `this` modifier. – Rahul Nikate May 13 '15 at 08:28
  • Thanks, i tried looking before posting the question but could not find any... – lysergic-acid May 13 '15 at 08:55

1 Answers1

2

Because you have to differentiate between extension method and normal static method. This is the key to say compiler that it is an extension otherwise you should invoke it like ExtensionMethods.Method(instance,arg,arg,...) (by the way you can).

MistyK
  • 6,055
  • 2
  • 42
  • 76
  • 1
    Not really. You have the instance parameter to differentiate it. – Sriram Sakthivel May 13 '15 at 08:26
  • No, because you can make normal static method without allowing it to be extension method. Example: public static string RemoveFirstLetter(string str) and you want to use it like that: Extensions.RemoveFirstLetter("hello") and you don't want to use it like that: "hello".RemoveFirstLetter(). If you introduce this keyword you will be able to use second option ALSO. – MistyK May 13 '15 at 08:28
  • 2
    There is no ambiguity here. For example if you have a static method in the class with same signature then the static methods wins, same applies to instance methods. I'm not sure what point you're trying to make. Also note that **OP's question is not why we have `this` keyword in declaration of extension method, question is why you need `this` keyword to call it**. – Sriram Sakthivel May 13 '15 at 08:32
  • I've never mentioned that there is an ambiguity. I am trying to say that there could be static methods which are not extensions methods. If there were no "this" keyword then all static method that contains parameter would be extension methods which is not what we expect to see. Let's image you have 50 utils functions with parameter of type string and now you write "hello". and what happens? You get long list of possible invocations from intelissense. Edit: Answering above: For compiler to say that it is ext method, it's like you have void keyword to say: method doesn't return parameter. – MistyK May 13 '15 at 08:35
  • Then just say it is for IntelliSense. Your current answer doesn't make sense. – Patrick Hofman May 13 '15 at 08:36
  • 1
    Please read my comment again(especially bold part). You seem to misunderstood the question. – Sriram Sakthivel May 13 '15 at 08:36
  • I'll recopy my edit: Answering above: For compiler to say that it is ext method, it's like you have void keyword to say: method doesn't return parameter. – MistyK May 13 '15 at 08:40