LA.NET [EN]

Mar 11

I’m back…I hope…After some complicated times which culminated in the floods which destroyed several regions of Madeira, I guess things are starting to coming back to normal…at least, that’s what I hope…I’ll probably return to blogging, though the rhythm won’t be the same as before.

One of the things I’ve been doing since I’ve migrated my code to C# 3.0 is defining and using extension methods. Another things which I tend *not* to do is use the this for qualifying access to a class’ members. Now, the thing is that *this* is required (ie, not optional) when you want to use an extension method in a class’ body. Suppose you’ve got class A and an extension method called T. Take a look at the following snippet:

class A {
    public void DoSomething() {
        this.T();
    }
}

Since T is an extension method, you do really need to use the *this* qualifier. If you don’t, you’ll end up with a compiler error. I guess this is one of the oddities associated with extension methods (after all, they’re really static methods and we’re talking about a hack which does really help in several scenarios)…

5 comments so far

  1. Rafael Romão
    5:33 pm - 3-11-2010

    Welcome back!

    For me the main oddity on using Extension Methods, until now, is the need to add a using clause for the namespace which contains the extension methods before to use them. But I see it as the cost to be paid.

    But why not to use the “this” qualifier?
    What oddities do you see on using it?

  2. Bertrand Le Roy
    10:50 pm - 3-11-2010

    Hold on, if you *know* about this extension method to yourself, why is this an extension method in the first place? Why not a simple method? Am I missing something?

  3. Bertrand Le Roy
    11:06 pm - 3-11-2010

    Hold on, if you *know* about this extension method to yourself, why is this an extension method in the first place? Why not a simple method? Am I missing something?

  4. luisabreu
    7:41 pm - 3-15-2010

    Bertrand, I”m following…the method was created for “extending” an existing class and the “problem” (ok, it”s not really a problem) happened when someone extended that class (ie, inherited from it) and needed to use the existing method internally. does this explanation gives you the right context?

  5. Bertrand Le Roy
    6:24 am - 3-17-2010

    Ah, OK, I see. Yes, looks like a reasonable scenario.