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.
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:
Without the “this” here the compiler wouldn’t know you mean the instance field.