Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can you not invoke extension methods directly?

Can someone explain to me why in the following the 3rd invocation of DoSomething is invalid? ( Error message is "The name 'DoSomething' does not exist in the current context" )

public class A { }
public class B : A
{
    public void WhyNotDirect()
    {
        var a = new A();
        a.DoSomething();  // OK
        this.DoSomething();  // OK
        DoSomething(); // ?? Why Not
    }
}
public static class A_Ext
{
    public static void DoSomething(this A a)
    {
        Console.WriteLine("OK");
    }
}
like image 680
My Other Me Avatar asked Oct 11 '10 12:10

My Other Me


3 Answers

Extension methods can be invoked like other static methods.

Change it to A_Ext.DoSomething(this).

If you're asking why it isn't implicitly invoked on this, the answer is that that's the way the spec was written. I would assume that the reason is that calling it without a qualifier would be too misleading.

like image 91
SLaks Avatar answered Nov 15 '22 13:11

SLaks


Because DoSomething takes a parameter.

DoSomething(a) would be legal.

Edit

I read the question a bit wrong here.

Since your calling it a a normal static method, and not a extension method, you need to prefic with the class name.

So A_Ext.DoSomething(a); will work.

If you call it like a normal static method, all the same rules apply.

Your second variant works because B inhetits A, and therefore you still end up calling it as an extension method, but the third does not.

sorry about the first version above that does not work. I'll leave it to keep the comment relevant.

like image 26
Øyvind Bråthen Avatar answered Nov 15 '22 12:11

Øyvind Bråthen


Extension methods are still static methods, not true instance calls. In order for this to work you would need specific context using instance method syntax (from Extension Methods (C# Programming Guide))

In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.

So while normally, both syntaxes would work, the second is without explicit context, and it would seem that the IL generated can't obtain the context implicitly.

like image 44
Joel Etherton Avatar answered Nov 15 '22 13:11

Joel Etherton