this Means the Current ObjectWhen C# is executing instance-level code that belongs to an object, the keyword:
this
refers to that current object.
If the current object is a particular Player, then:
this
means that Player object.
If the current object is a particular Team, then:
this
means that Team object.
The meaning changes with the object whose code is currently executing.
this to an Object DiagramSuppose a UML object diagram contains:
player1 : Player
-------------------------
name = "Jordan"
jerseyNumber = 7
At runtime, when instance code is executing for the object represented by player1, this refers to that same current Player object.
Conceptually:
player1 : Player
↑
this
this is not a second object.
It is a way for the current object's code to refer to itself.
this to Access the Current Object's FieldsSuppose the supplied Player class has fields:
string name;
int jerseyNumber;
Inside instance-level code for a Player object, you may encounter:
this.name = "Jordan";
this.jerseyNumber = 7;
Read the first assignment as:
Set the
namefield on the current Player object to"Jordan".
Read the second as:
Set the
jerseyNumberfield on the current Player object to7.
The word this answers:
Which object owns this field?
this.field Is Different From Another Object's FieldCompare:
this.name
with:
player2.name
The first refers to the name field on the current object.
The second refers to the name field on the object reached through player2.
If the current object is player1, then conceptually:
this.name → player1's name
player2.name → player2's name
Those can be different values because the objects are different.
this Does Not Create an ObjectThis code:
Player player1 = new Player();
creates a Player object.
The keyword:
this
does not create anything.
It only refers to the object whose instance-level code is already running.
Keep these ideas separate:
new
Creates an object.
this
Refers to the current object.
this Can Make Ownership ExplicitSometimes C# allows a field to be referenced without writing this..
For example, inside the current Player object's code:
name = "Jordan";
may refer to the current object's name field.
Writing:
this.name = "Jordan";
makes the object ownership explicit:
the
namefield on this object
This can be useful while learning object-oriented code because the relationship between the object and its member is visible.
this Is Especially Helpful When Names Could Be ConfusingImagine code where a temporary value and a field have similar names.
A reference such as:
this.name
clearly identifies the field that belongs to the current object.
You do not need to redesign the supplied code to add this everywhere.
Read it when it appears and use it where the current course task calls for it.
this in the DebuggerWhen execution pauses inside instance-level code, the debugger can help you inspect the current object.
If you expand the current object, you may see fields such as:
name
jerseyNumber
isAvailable
Those are the same members that expressions such as:
this.name
this.jerseyNumber
this.isAvailable
refer to.
The debugger makes the current object's state visible while this gives the source code a way to refer to that state.
this as a SentenceWhen you see:
this.jerseyNumber = 7;
read:
On the current object, set
jerseyNumberto 7.
When you see:
this.isAvailable = true;
read:
On the current object, set
isAvailableto true.
That reading keeps the object and the member connected.
this answers one important object-oriented question:
Which object am I talking about from inside this object's own code?
The answer is:
the current object.
The next activity focuses on the dot operator that connects an object reference such as this or player1 to one of its accessible members.