27

可能重复:
您何时使用“this”关键字?

你好,我知道This关键字是用来引用类的一个实例,但是,假设我有一个名为的类Life,它定义了两个字段,人(他们的名字)和他们的伙伴(他们的名字):

class Life
{
    //Fields
    private string _person;
    private string _partner;

    //Properties
    public string Person
    {
        get { return _person; }
        set { _person = value; }
    }

    public string Partner
    {
        get { return _partner; }
        set { _partner = value; }
    }

    //Constructor 1
    public Life()
    {
        _person = "Dave";
        _partner = "Sarah";

        MessageBox.Show("Life Constructor Called");
    }

    //Constructor 2
    public Life()
    {
        this._person = "Dave";
        this._partner = "Sarah";

        MessageBox.Show("Life Constructor Called");
    }
}

构造函数1和构造函数2有区别吗!?还是使用“This”关键字只是更好的编码习惯?

问候

4

8 回答 8

23

构造函数是相同的。 我更喜欢第二个的原因是它允许您从私有变量名称中删除下划线并保留上下文(提高可理解性)。我习惯于this在引用实例变量和属性时始终使用它。

this转到不同标准的不同公司后,我不再以这种方式使用关键字。我已经习惯了,现在在提及实例成员时很少使用它。我仍然建议使用属性(显然)。

我的你的班级版本:

class Life
{
    //Fields
    private string person;
    private string partner;

    //Properties
    public string Person
    {
        get { return this.person; }
        set { this.person = value; }
    }

    public string Partner
    {
        get { return this.partner; }
        set { this.partner = value; }
    }


    public Life()
    {
        this.person = "Dave";
        this.partner = "Sarah";

        MessageBox.Show("Life Constructor Called");
    }
}

this或者,甚至更好,但对with 字段的使用不是很清楚。

class Life
{

    //Properties
    public string Person { get; set; }
    public string Partner { get; set; }

    public Life()
    {
        this.Person = "Dave";
        this.Partner = "Sarah";

        MessageBox.Show("Life Constructor Called");
    }
}
于 2009-05-09T13:17:19.480 回答
13

"this" 也用于 .Net 3.5 的扩展方法:

public static class MyExtensions
{    
    public static string Extend(this string text)
    {
       return text + " world";
    }
}

将扩展字符串类

var text = "Hello";
text.Extend();

回答您的问题:不,您的两个构造函数没有区别。Imo,“this”使代码混乱,只应在必要时使用,例如,当参数和字段变量具有相同名称时。

还有一种情况是类显式实现了接口。如果您需要从类中调用接口方法,则必须将其强制转换为接口:

class Impl : IFace
{

    public void DoStuff()
    {
        ((IFace)this).SomeMethod();
    }

    void IFace.SomeMethod()
    {
    }
}
于 2009-05-09T13:26:08.550 回答
7

两种说法没有区别...

//These are exactly the same.

this._person 

//and 

_person 

在 _person 的情况下暗示了对“this”的引用。我不会说这一定是“更好”的编码实践,我会说这只是偏好。

于 2009-05-09T13:20:24.100 回答
3

已经讨论过了

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

于 2009-05-09T13:19:08.840 回答
3

由于您使用的是下划线,因此名称之间没有冲突;所以“ this.”是多余的,可以省略。IL 将不受影响。

只要字段和变量/参数之间没有歧义,就只有一种情况,其中this关键字(在表示当前实例的上下文中 - 不是 ctor 链接)是严格必要的 - 调用定义的扩展方法分别地:

this.SomeExtensionMethod();  // works
SomeExtensionMethod();  // fails
于 2009-05-09T13:25:55.510 回答
1

Both constructors do the same thing anyway in the second one the this is redundant

于 2009-05-09T13:28:50.533 回答
0

您可以使用它来区分名为 X 的局部变量和同名的类级别字段/属性。

于 2009-05-09T13:19:39.123 回答
0

您不应该使用私有变量 _person 和 _parter。这就是你的 getter 和 setter 的目的。

就结构而言,它们之间没有真正的区别。话虽如此,我总是更喜欢使用 This 关键字,因为它有助于提高可读性。

于 2009-05-09T13:21:53.633 回答