1.1.12 Accessing Object Members With the Dot Operator

The Dot Connects an Object to One of Its Members

You have already seen expressions such as:

C#
player1.name

The dot:

Plain text
.

is the member-access operator.

It connects an object reference with a member available through that object.

Read:

C#
player1.name

as:

the name member belonging to the object referenced by player1

Begin on the Left Side of the Dot

Consider:

C#
player1.jerseyNumber

The left side is:

Plain text
player1

That identifies the object reference you are starting from.

The right side is:

Plain text
jerseyNumber

That identifies the member you want to access.

A useful reading order is:

Plain text
which object? → which member?

So:

C#
player1.jerseyNumber

becomes:

On player1, access jerseyNumber.

The Same Member Name Can Exist on Different Objects

Suppose:

C#
Player player1 = new Player();
Player player2 = new Player();

Both Player objects can have a field named:

Plain text
name

These expressions refer to different object state:

C#
player1.name
player2.name

The member name is the same.

The object reference before the dot determines which object's member is being accessed.

The Dot Makes UML-to-Code Connections Visible

Suppose the UML contains:

Plain text
player1 : Player
-------------------------
name = "Jordan"

A C# assignment can be:

C#
player1.name = "Jordan";

The dot expresses the relationship:

Plain text
player1 → name

That is the code-level way of saying:

This field value belongs to this specific object.

this Can Appear on the Left Side

Inside the current object's instance-level code, you may see:

C#
this.name

The left side:

Plain text
this

means the current object.

The right side:

Plain text
name

is the member being accessed.

The same dot rule applies.

Conceptually:

Plain text
this.name

means:

the name field of the current object

Accessing a Member Does Not Automatically Change It

This expression:

C#
player1.name

identifies a member.

It does not change the member by itself.

A field changes when code performs an assignment such as:

C#
player1.name = "Jordan";

Keep the operations separate:

member access

C#
player1.name

field assignment

C#
player1.name = "Jordan";

The next activity focuses directly on assignment.

The Member Must Belong to the Object's Type

If player1 refers to a Player, then the available members come from the supplied Player type and what C# makes accessible through it.

You cannot invent:

C#
player1.favoritePlanet

unless that member actually exists and is accessible on the supplied type.

When the compiler reports that a member cannot be found, inspect:

Do not add a new field merely to make the error disappear unless the current requirement tells you to define one.

Member Names Must Be Exact

C# distinguishes identifiers precisely.

If the field is:

Plain text
jerseyNumber

then:

C#
player1.jerseyNumber

and:

C#
player1.jerseynumber

are not automatically the same identifier.

Preserve the member names used by the supplied project.

The Left Side Must Refer to an Object Before Instance Members Can Be Used

Suppose:

C#
Player player1 = null;

Then:

C#
player1.name

attempts to access a member through a reference that does not currently point to a Player object.

That can lead to a NullReferenceException when the statement executes.

For now, remember the basic dependency:

Plain text
object reference
      ↓
dot operator
      ↓
member

The object reference needs to identify an actual object when the program uses that instance member.

You will diagnose null-reference problems in CO-029.

Dot Access Can Continue Through Related Objects

Later in Module 1.1, you will encounter expressions that move through one object to another related object.

For now, focus on one step:

C#
player1.name

or:

C#
team1.name

Understand one object/member connection before following longer chains.

The Main Reading Pattern

Whenever you see:

Plain text
something.member

ask:

  1. What does something refer to?
  2. What member is being accessed?
  3. Am I only reading the member, or is the larger statement assigning to it?
  4. Does the supplied type actually contain that member?

The dot operator is small punctuation, but it is one of the most important ways C# expresses object-oriented structure.