可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Possible Duplicate:
When do you use the “this” keyword?
Hello,
I understand that the This
keyword is used to refer to an instance of the class, however, suppose I have a class called Life
, which defines two fields, the person (their name) and their partner(their name):
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");
}
}
Is there a difference between constructor 1 and constructor 2!?
Or is it just better coding practice to use the "This" keyword?
Regards
回答1:
The constructors are the same. The reason I would prefer the second is that it will allow you to remove the underscores from your private variable names and retain the context (improving understandability). I make it a practice to always use this
when referring to instance variables and properties.
I no longer use the this
keyword in this way after moving to a different company with different standards. I've gotten used to it and now rarely use it at all when referring to instance members. I do still recommend using properties (obviously).
My version of your class:
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");
}
}
or, even better, but not as clear about the use of this
with fields.
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");
}
}
回答2:
"this" is also used in .Net 3.5 with extension methods:
public static class MyExtensions
{
public static string Extend(this string text)
{
return text + " world";
}
}
would extend the string class
var text = "Hello";
text.Extend();
To answer your question: no, there is no difference in your two constructors. Imo, the "this" clutters the code and should only be used when necessary, e.g. when parameters and field variables have the same names.
There is also a case when the class explicitly implements an interface. If you need to call the interface methods from within your class you would have to cast this to the interface:
class Impl : IFace
{
public void DoStuff()
{
((IFace)this).SomeMethod();
}
void IFace.SomeMethod()
{
}
}
回答3:
There is no difference in the two statements...
//These are exactly the same.
this._person
//and
_person
The reference to "this" is implied in the case of _person. I wouldn't say that it is necessarily "better" coding practice, I would say that it is just preference.
回答4:
Already discussed
When do you use the "this" keyword?
回答5:
Since you are using underscores, there is no conflict between the names; so the "this.
" is redundant and can be omitted. The IL will be unaffected.
As long as there is no ambiguity between a field and variable/parareter, there is only one scenario in which the this
keyword (in the context of meaning the current instance - not ctor-chaining) is strictly necessary - invoking an extension method that is defined separately:
this.SomeExtensionMethod(); // works
SomeExtensionMethod(); // fails
回答6:
Both constructors do the same thing anyway in the second one the this
is redundant
回答7:
You can use this to differentiate between a local variable named X and a class level field/property of the same name.
回答8:
You shouldn't be using the private variables _person and _parter. That is the purpose of your getters and setters.
As far as the constructs, there is no real difference between them. That being said, I always prefer to use the This keyword as it lends towards readability.