2

What is the difference b/w the following two codes. What is the use of using 'this' keyword in the constructor.

Ex 1:

public Product(string name){
this.Name = name;
}

Ex 2:

public Product(string name){
Name = name;
}

I know this refers to the calling object. Just I couldn't able to get the difference?

can some one please explain?

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
Bhaskar
  • 1,680
  • 7
  • 26
  • 40
  • "To qualify members hidden by similar names". It is explained here: http://msdn.microsoft.com/en-gb/library/dk1507sz(v=vs.90).aspx – Saturnix Sep 06 '13 at 09:05

7 Answers7

6

Example 1 uses an explicit reference to this. This is not really needed as there's no ambiguity in this case. If the parameter was called Name instead, the explicit this would be required to resolve the ambiguity.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
4

It is absolutely optional in the example you provide.

Mainly, it implicitely tells the user that the member that is worked with is part of the current class object.

One practice was to differentiate parameters from members or fields like the following:

public class Customer {
    private string name;

    public Customer(string name) {
        this.name = name;
    } 
}

For readability's sake, one would put this in front of the name field to make it clear that the value of the name parameter is assigned to the this class' name field.

In such situation, it is necessery, as both the constructor parameter and the field have the same name. Otherwise, if they were different variable names, the this keyword would be optional.

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
3

In that case, there's no difference. The more common reason for being explicit is something like this:

public Product(string name){
    this.name = name;
}

At that point the this is absolutely required, otherwise it would be a no-op assignment from the parameter to itself (which would raise a warning, IIRC).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

In terms of emitted IL absolutely no difference. It's just for readability. When you use this you are more explicit.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

this.Name is more explicit than just Name. Using this makes it more obvious as to what the code is doing; but in this case the compiler will interpret them the same way.

Donut
  • 110,061
  • 20
  • 134
  • 146
1

I disagree with other here who say using "this" is more explicit. It is most definitely not. In the examples you gave, the property "Name" is as explicit as it gets. If we had another keyword that had more letters than "this" it wouldn't be more explicit.

Name

is no less explicit than

this.Name

which is also no less explicit than the following nonsense

thisIsNotAKeyWordButItMeansThis.Name
Pedro
  • 382
  • 2
  • 7
0

It is a matter of style. Like the others have said, there is no difference in the IL. Personally, I like to see the this. prefix used whenever accessing members of the class. In fact, the default StyleCop rules require it. It just makes it clear where the member is coming from.

Bryan
  • 2,775
  • 3
  • 28
  • 40