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();
}