views:

319

answers:

5

I have created an extension method for an ASP.NET MVC ViewPage, e.g:

public static class ViewExtensions
{
    public static string Method<T>(this ViewPage<T> page) where T : class
    {
        return "something";
    }
}

When calling this method from the ViewPage, I get the error "CS0103: The name 'Method' does not exist in the current context" unless I use the this keyword to call it:

<%: Method() %> <!-- gives error CS0103 -->
<%: this.Method() %> <!-- works -->

Why is the this keyword required? Or does it work without it, but I'm missing something?

(I think there must be a duplicate of this question, but I was not able find one)

Update:

As Ben Robinson says, the syntax to call extension methods is just compiler sugar. Then why can't the compiler check for extension methods without requiring the this keyword?

+3  A: 

Without it the compiler just sees it as a static method in a static class which takes page as it's first parameter. i.e.

// without 'this'
string s = ViewExtensions.Method(page);

vs.

// with 'this'
string s = page.Method();
Ferruccio
I think that's supposed to be ViewExtensions.Method(page).
Dave Van den Eynde
@Dave Van den Eynde: thanks. fixed.
Ferruccio
+1  A: 

Because the extension method does not exist with the ViewPage class. You need to tell the compiler what you are calling the extension method on. Remember this.Method() is just compiler sugar for ViewExtensions.Method(this). It is the same way you can't just call an extention method within the middle of any class by the method name.

Ben Robinson
This makes sense (somehow). But if it's compiler sugar, then why can't the compiler check for extension methods without the `this` keyword?
M4N
Looks like an oversight to me. As you said - the compiler could see the method does not exist, then check extensions for availability.
TomTom
Yeah arguably this could work. I would suspect it is a specific design decision to make the code more readable. If you could simply call ExtensionMethod() within a class it might be a bit confusing to someone reading the code if that class doesn't contain that method but with this.ExtensionMethod() at least it is consistent with using MyInstance.ExtentionMethod()
Ben Robinson
I think that this.Method() is more like syntactic sugar for ViewExtensions.Method(this).
Alex Humphrey
Yes you are right i have corrected it.
Ben Robinson
+2  A: 

It's important to note that there are differences between extension methods and regular methods. I think you've just come across one of them.

I'll give you an example of another difference: It's fairly easy to call an extension method on a null object reference. Fortunately, this is much more difficult to do with regular methods. (But it can be done. IIRC, Jon Skeet demonstrated how to do this by manipulating CIL code.)

static void ExtensionMethod(this object obj) { ... }

object nullObj = null;
nullObj.ExtensionMethod();  // will succeed without a NullReferenceException!

That being said, I agree that it seems a little unlogical that this is required to call the extension method. After all, an extension method should ideally "feel" and behave just like a normal one.

But in reality, extension methods are more like syntactic sugar added on top of the existing language than an early core feature that fits nicely into the language in all respects.

stakx
in Boo language you can invoke Extension method or property directly on null i.e. null.Do().
Sergey Mirvoda
@Sergey: Sounds dangerous... :) Makes me curious how Boo knows the (implied) type of `null`? Could be just about any reference type, how does it know what methods are available on `null`?
stakx
There may be a good reason to be able to call an extension method with a null reference.
Dave Van den Eynde
A: 

On instance methods, 'this' is implicitly passed to each method transparently, so you can access all the members it provides.

Extension methods are static. By calling Method() rather than this.Method() or Method(this), you're not telling the compiler what to pass to the method.

You might say 'why doesn't it just realise what the calling object is and pass that as a parameter?'

The answer is that extension methods are static and can be called from a static context, where there is no 'this'.

I guess they could check for that during compilation, but to be honest, it's probably a lot of work for extremely little payoff. And to be honest, I see little benefit in taking away some of the explicitness of extension method calls. The fact that they can be mistaken for instance methods means that they can be quite unintuitive at times (NullReferenceExceptions not being thrown for example). I sometimes think that they should have introduced a new 'pipe-forward' style operator for extension methods.

Alex Humphrey
+12  A: 

A couple points:

First off, the proposed feature (implicit "this." on an extension method call) is unnecessary. Extension methods were necessary for LINQ query comprehensions to work the way we wanted; the receiver is always stated in the query so it is not necessary to support implicit this to make LINQ work.

Second, the feature works against the more general design of extension methods: namely, that extension methods allow you to extend a type that you cannot extend yourself, either because it is an interface and you don't know the implementation, or because you do know the implementation but do not have the source code.

If you are in the scenario where you are using an extension method for a type within that type then you do have access to the source code. Why are you using an extension method in the first place then? You can write an instance method yourself if you have access to the source code of the extended type, and then you don't have to use an extension method at all! Your implementation can then take advantage of having access to the private state of the object, which extension methods cannot.

Making it easier to use extension methods from within a type that you have access to is encouraging the use of extension methods over instance methods. Extension methods are great, but it is usually better to use an instance method if you have one.

Given those two points, the burden no longer falls on the language designer to explain why the feature does not exist. It now falls on you to explain why it should. Features have enormous costs associated with them. This feature is not necessary and works against the stated design goals of extension methods; why should we take on the cost of implementing it? Explain what compelling, important scenario is enabled by this feature and we'll consider implementing it in the future. I don't see any compelling, important scenario that justifies it, but perhaps there is one that I've missed.

Eric Lippert
Thanks for that answer! Two points:1: I was not asking for that feature to be implemented, just for an explanation, why it isn't there / why `this` is required.2: Why do I call an extension method from the extended type? In the example given, I'm adding some convenience methods to the base class (ViewPage) and I'm calling it from derived classes. This eliminates the need of creating a common base class for all my views.BTW: I agree that using an extension method from within the extended type is awkward, but again see the example with MVC ViewPage.
M4N
I don't think the reason that extension methods were needed for LINQ is a good justification, as its not the only reason why one might use them, now that they exist. This doesn't stop me agreeing with the rest of the answer that implicit this is unnecessary. I can see one use for implicit this (implementing an interface or derived class for which an extension method already exists), but even here that extension methods "work from the outside" is reflected is a good thing IMO.
Jon Hanna