我很好奇其他人如何使用this关键字.我倾向于在构造函数中使用它,但我也可以在其他方法中使用它.一些例子:
在构造函数中:
public Light(Vector v)
{
this.dir = new Vector(v);
}
Run Code Online (Sandbox Code Playgroud)
别处
public void SomeMethod()
{
Vector vec = new Vector();
double d = (vec * vec) - (this.radius * this.radius);
}
Run Code Online (Sandbox Code Playgroud) 在以下构造函数中,是否需要'this'关键字?我知道我可以删除它,它符合,一切都很好.如果我省略'this'那将导致我的问题在路上?是否认为"这个"被认为是不好的做法?
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
Run Code Online (Sandbox Code Playgroud)