你在C#中使用“this”运算符吗?

时间:2008-11-21 01:41:20

标签: c# .net programming-languages

重复发帖,请参阅:When do you use the "this" keyword?


在我工作的几乎每个项目中都使用了“This”操作符,当我开始开发时,我被告知这是一个很好的做法。这是否真的有必要为您提供更多可读性?

1 个答案:

答案 0 :(得分:0)

像Resharper这样的工具有一个内置提示,说“冗余限定符”,但我不同意它并快速禁用该规则。

我总是使用这个限定符,因为它让我一眼就知道引用是属性/字段,还是静态类引用,例如:

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