This project is read-only.

Allow using Extension Methods with implicit this

Topics: C# Language Design
Sep 26, 2014 at 9:27 AM
Edited Sep 26, 2014 at 9:29 AM
If I have some ExtensionMethod
static void Foo( this ThirdPartyClass tpc ){...}
inside SomeClass I must to use "this" explicitly to call to ExtensionMethod
class SomeClass : ThirdPartyClass
{
    ...
    void Bar()
    {
        Foo( );//Error Foo does not exists in current context
        this.Foo( );//Correct
    }
    ...
}
So Allow to use Foo with implicit this.
Sep 28, 2014 at 2:23 AM
This would kind of be nice. I've written various extension methods to extend Entity Framework entities through annotations extending EntityTypeConfiguration<TEntity>. It's common to define a subclass of that type for each configured entity and call methods of the base class to set the configuration metadata. In the case of these extension methods they have to be prefixed by this as you mention and it definitely looks awkward and is less discoverable to developers consuming the API as intended.
Sep 29, 2014 at 5:53 PM
Given static void Foo(this Thing it, params Thing[] more) {...} what would be the meaning, within class Thing, of Foo(thing1, thing2);? Would it be equivalent to Thing.Foo(thing1, thing2); or Thing.Foo(this, thing1, thing2);? In cases where either an extension method or an "ordinary" method could be bound, the extension method is supposed to be silently ignored rather than producing an "ambiguous invocation" message, but I would not consider it at all clear that the programmer intended Thing.Foo(thing1, thing2); rather than Thing.Foo(this, thing1, thing2);.
Sep 30, 2014 at 1:02 AM
supercat wrote:
Given static void Foo(this Thing it, params Thing[] more) {...} what would be the meaning, within class Thing, of Foo(thing1, thing2);? Would it be equivalent to Thing.Foo(thing1, thing2); or Thing.Foo(this, thing1, thing2);? In cases where either an extension method or an "ordinary" method could be bound, the extension method is supposed to be silently ignored rather than producing an "ambiguous invocation" message, but I would not consider it at all clear that the programmer intended Thing.Foo(thing1, thing2); rather than Thing.Foo(this, thing1, thing2);.
Good one!
Sep 30, 2014 at 2:44 AM
supercat wrote:
Given static void Foo(this Thing it, params Thing[] more) {...} what would be the meaning, within class Thing, of Foo(thing1, thing2);? Would it be equivalent to Thing.Foo(thing1, thing2); or Thing.Foo(this, thing1, thing2);? In cases where either an extension method or an "ordinary" method could be bound, the extension method is supposed to be silently ignored rather than producing an "ambiguous invocation" message, but I would not consider it at all clear that the programmer intended Thing.Foo(thing1, thing2); rather than Thing.Foo(this, thing1, thing2);.
I'm not sure if I am reading you correctly but it sounds like you're describing the ambiguity as between calling either the static method as an extension method or the static method as a static method from an instance method of the current type. Is that about right?
public class Thing
{
    public static void Foo(this Thing it, params Thing[] more) { }

    public Thing(Thing thing1, Thing thing2)
    {
        Foo(thing1, thing2);
    }
}