I have added an extension method to the ASP.NET System.Web.UI.Page.
Every page in my application inherits from this class.
I cannot just access the extension method, however. I must type this.MyMethod(); instead of just being able to use MyMethod(). I thought that methods/attributes of this were inherently in the default scope. What am I not understanding? Or is this a nuance of extension methods?
They are. But extension methods are not methods of
thisin the default scope; they are static methods accessible via syntactic sugar provided as a courtesy by the compiler.I believe you already know this, but just to clarify: if
ExtensionMethodis an extension method of the class whose scope you’re currently in, typing this:…is the same as:
thisneeds to be passed as a parameter toExtensionMethod. The first way above, the compiler simply does this for you.Naturally, they could have implemented things differently so that the compiler “brings in” extension methods as well as class members into the default scope; they just simply chose not to. Personally, I like that; but I guess it’s a subjective matter. Anyway, if you dislike having to type
this, it’s only a small annoyance, right?