相关疑难解决方法(0)

你什么时候使用"this"关键字?

我很好奇其他人如何使用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)

c# coding-style this

248
推荐指数
11
解决办法
19万
查看次数

在C#中,是否需要"this"关键字?

在以下构造函数中,是否需要'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)

c#

6
推荐指数
3
解决办法
2996
查看次数

标签 统计

c# ×2

coding-style ×1

this ×1