<分区>
考虑这个类:
public class Thing {
public string Color { get; set; }
public bool IsBlue() {
return this.Color == "Blue"; // redundant "this"
}
}
我可以省略关键字 this
因为Color
是 Thing
的属性, 我在 Thing
内编码.
如果我现在创建一个扩展方法:
public static class ThingExtensions {
public static bool TestForBlue(this Thing t) {
return t.Color == "Blue";
}
}
我现在可以更改我的 IsBlue
方法:
public class Thing {
public string Color { get; set; }
public bool IsBlue() {
return this.TestForBlue(); // "this" is now required
}
}
但是,我现在需要包含 this
关键字。
我可以省略 this
在引用属性和方法时,为什么我不能这样做...?
public bool IsBlue() {
return TestForBlue();
}