Reputation: 41128
What is the difference between:
public class Person
{
public string Name { get; set; }
public Person(string name)
{
this.Name = name;
}
}
public class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}
Don't they both do the exact same thing?
Thank you.
Upvotes: 2
Views: 294
Reputation: 7096
They compile the same, but one thing you'll notice is that in VC#, IntelliSense will give you a list of the relevant fields/properties if you type "this." first. Given that you type reasonably fast, I think typing "this." ends up saving you a little bit of thinking time.
Upvotes: 0
Reputation: 526
as an addition:
even though they do the same, i'd recommend using public properties as an interface to the private member, for internal references always use the private field name.
Upvotes: 0
Reputation: 12833
Yes they do the same thing.
In addition to clarifying that you are dealing with an instance of the class and distinguishing the field from the parameter, you also get some intellisense goodness when you use this and then hit the dot.
Another matter of taste that you may come across is to distinguish the field from the parameter by prefixing an underscore in front of the field and not using this, as in:
_name = name;
This convention also is useful with Intellisense as you know you will find all of your private fields at the top of the list.
Finally, it isn't uncommon at all to use a private setter in your property and eliminate the backing field altogether, as in:
public string Name { get; private set; }
public Person(string name)
{
Name = name;
}
Which would bring us full circle to the this operator, which if used in front of Name (this.Name) would compile to the exact same thing.
My personal taste is to not use this, as I find it noisy to do so (and if I needed it to distinguish one thing from another I have surely screwed something else up). Plenty of people who write very elegant code will use it though.
HTH,
Berryl
Upvotes: 0
Reputation: 16752
Yes, they do. In the second code sample the "this" is implicit. When you use the "this." you are being more explicit in saying that you are referring to a instance member.
There are times when you need to use the this keyword. For example:
public class Person
{
public string name;
public Person(string name)
{
this.name = name;
}
}
Without the "this" here the compiler wouldn't know you mean the instance field.
Upvotes: 12
Reputation: 46913
The two pieces of code you have shown do the same thing.
However, if you had this, it would be different:
public class Person
{
public string Name { get; set; }
public Person(string name)
{
String Name;
this.Name = name;
}
}
Note the local variable with the same identifier. Many people explicitly use this
to refer to member variables for readability and to avoid situations such as the above.
Upvotes: 1
Reputation: 60902
There is no difference; they both do the same thing, and compile to the same IL.
I personally prefer to use the form
this.Name = name;
...to clarify my intent. If the parameter were named the same as the member field (casing and all), then you'd have to use this
to disambiguate which one you were referring to.
Upvotes: 10
Reputation: 14391
Yes they both do the same thing but using "this" explicitly states that you are dealing with the current instance of the class http://msdn.microsoft.com/en-us/library/dk1507sz(VS.71).aspx
"this" can allow you to qualify a member, especially useful when they are identical.
this.name = name;
Upvotes: 2
Reputation: 59453
Yes, semantically the two pieces of code are identical.
The reason for the first version is that it's common to put this
in front of a class member in a constructor, because commonly a constructor parameter name will be the same as the member name, and this
must be used to distinguish them. Since the backing store for this property is automatically generated, and there is no private field member defined, the names have different capitalization, and thus this
is not required.
public class Person
{
private string name;
public string Name { get { return name; } set { name = value; } }
public Person(string name)
{
this.name = name; // "this" is required in this case.
}
}
Upvotes: 1
Reputation: 18944
They do indeed. It's a matter of taste. Some people like to say "hey look I am talking to a member variable! On purpose!". Some people have habits related to triggering intellisense. The compiler doesn't care whether you type it or not. In some strange cases where you have variables with the same names in different scopes you need the this.
to make it clear, but your example isn't one of those cases.
Upvotes: 1