我很好奇其他人是如何使用这个关键字的。我倾向于在构造函数中使用它,但我也可能在整个类的其他方法中使用它。一些例子:

在构造函数中:

public Light(Vector v)
{
    this.dir = new Vector(v);
}

在其他地方

public void SomeMethod()
{
    Vector vec = new Vector();
    double d = (vec * vec) - (this.radius * this.radius);
}

当前回答

从来没有。永远。如果你有变量阴影,你的命名约定就会崩溃。我的意思是,成员变量没有区别的命名吗?Facepalm指

其他回答

我用它来调用智能感知就像JohnMcG,但我会回去擦除“this->”当我完成。我遵循微软的惯例,在成员变量前加上“m_”,所以把它作为文档是多余的。

只要StyleCop告诉我,我就使用它。必须遵守StyleCop。噢,是的。

你应该经常使用它,我用它来区分私有字段和参数(因为我们的命名约定声明我们不为成员和参数名使用前缀(并且它们是基于在互联网上找到的信息,所以我认为这是一个最佳实践))

每当我引用一个实例变量时,我都会使用它,即使我不需要。我认为这让代码更清晰。

我使用它的时候,在一个函数中,接受了对同一类型对象的引用,我想完全清楚我引用的是哪个对象,在哪里。

例如

class AABB
{
  // ... members
  bool intersects( AABB other )
  {
    return other.left() < this->right() &&
           this->left() < other.right() &&

           // +y increases going down
           other.top() < this->bottom() &&
           this->top() < other.bottom() ;
  }
} ;

(vs)

class AABB
{
  bool intersects( AABB other )
  {
    return other.left() < right() &&
           left() < other.right() &&

           // +y increases going down
           other.top() < bottom() &&
           top() < other.bottom() ;
  }
} ;

AABB的right()指的是哪一个?这增加了一点澄清。