重复帖子,请参阅:您何时使用“this”关键字?
在我工作的几乎每个项目中,都会使用“This”运算符,当我开始开发时,我被告知这是一个好习惯。这真的有必要吗?它会给你更多的可读性吗?
像 Resharper 这样的工具有一个内置提示说“冗余限定符”,但我不同意它并迅速禁用该规则。
我总是使用 this 限定符,因为它让我一眼就知道引用是属性/字段还是静态类引用,例如:
public class MyClass {
public int Foo { get; set; }
}
public MyClass MyRef { get; }
或者
public static class MyRef {
public static int Foo { get; set; }
}
所以:
void method() {
MyRef.Foo = 4; // might be either
}
void method() {
this.MyRef.Foo = 4; // definitely property/field
}
只是我的2c。
-Oisin