Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using extension methods from within the class they extend [duplicate]

Consider this class:

public class Thing {

    public string Color { get; set; }

    public bool IsBlue() {
        return this.Color == "Blue";   // redundant "this"
    }

}

I can omit the keyword this because Color is a property of Thing, and I'm coding within Thing.

If I now create an extension method:

public static class ThingExtensions {

    public static bool TestForBlue(this Thing t) {
        return t.Color == "Blue";
    }

}

I can now change my IsBlue method to this:

public class Thing {

    public string Color { get; set; }

    public bool IsBlue() {
        return this.TestForBlue();   // "this" is now required
    }
}

However, I'm now required to include the this keyword.

I can omit this when referencing properties and methods, so why can't I do this...?

public bool IsBlue() {
    return TestForBlue();
}
like image 939
BG100 Avatar asked Sep 10 '13 15:09

BG100


People also ask

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

What is the extension method for a class?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Can extension methods extend non static classes?

It is compulsion that the Extension method must be in a Static class only so that only one Instance is created. For example, if you place the following in ASP.Net page it will not work.

What is extension method in Java?

In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages.


1 Answers

I can omit this when referencing properties and methods, so why can't I do this...?

It's just part of how extension methods are invoked, basically. Section 7.6.5.2 of the C# specification (Extension Method Invocations) starts:

In a method invocation (7.5.5.1) of one of the forms

expr . identifier ( )
expr . identifier ( args )
expr . identifier < typeargs > ( )
expr . identifier < typeargs > ( args )

if the normal processing of the invocation finds no applicable methods, an attempt is made to process the construct as an extension method invocation.

Without the this, your invocation wouldn't be of that form, so that section of the spec wouldn't apply.

This isn't a justification of why the feature was designed that way, of course - it's a justification of the compiler's behaviour in terms of correctness.

like image 131
Jon Skeet Avatar answered Sep 30 '22 15:09

Jon Skeet