1.1.11 Referencing the Current Object With this

this Means the Current Object

When C# is executing instance-level code that belongs to an object, the keyword:

C#
this

refers to that current object.

If the current object is a particular Player, then:

C#
this

means that Player object.

If the current object is a particular Team, then:

C#
this

means that Team object.

The meaning changes with the object whose code is currently executing.

Connect this to an Object Diagram

Suppose a UML object diagram contains:

Plain text
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:

Plain text
player1 : Player
      ↑
     this

this is not a second object.

It is a way for the current object's code to refer to itself.

Use this to Access the Current Object's Fields

Suppose the supplied Player class has fields:

C#
string name;
int jerseyNumber;

Inside instance-level code for a Player object, you may encounter:

C#
this.name = "Jordan";
this.jerseyNumber = 7;

Read the first assignment as:

Set the name field on the current Player object to "Jordan".

Read the second as:

Set the jerseyNumber field on the current Player object to 7.

The word this answers:

Which object owns this field?

this.field Is Different From Another Object's Field

Compare:

C#
this.name

with:

C#
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:

Plain text
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 Object

This code:

C#
Player player1 = new Player();

creates a Player object.

The keyword:

C#
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 Explicit

Sometimes C# allows a field to be referenced without writing this..

For example, inside the current Player object's code:

C#
name = "Jordan";

may refer to the current object's name field.

Writing:

C#
this.name = "Jordan";

makes the object ownership explicit:

the name field 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 Confusing

Imagine code where a temporary value and a field have similar names.

A reference such as:

C#
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.

Follow this in the Debugger

When 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:

Plain text
name
jerseyNumber
isAvailable

Those are the same members that expressions such as:

C#
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.

Read this as a Sentence

When you see:

C#
this.jerseyNumber = 7;

read:

On the current object, set jerseyNumber to 7.

When you see:

C#
this.isAvailable = true;

read:

On the current object, set isAvailable to true.

That reading keeps the object and the member connected.

The Main Idea

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.