You have already seen expressions such as:
player1.name
The dot:
.
is the member-access operator.
It connects an object reference with a member available through that object.
Read:
player1.name
as:
the
namemember belonging to the object referenced byplayer1
Consider:
player1.jerseyNumber
The left side is:
player1
That identifies the object reference you are starting from.
The right side is:
jerseyNumber
That identifies the member you want to access.
A useful reading order is:
which object? → which member?
So:
player1.jerseyNumber
becomes:
On
player1, accessjerseyNumber.
Suppose:
Player player1 = new Player();
Player player2 = new Player();
Both Player objects can have a field named:
name
These expressions refer to different object state:
player1.name
player2.name
The member name is the same.
The object reference before the dot determines which object's member is being accessed.
Suppose the UML contains:
player1 : Player
-------------------------
name = "Jordan"
A C# assignment can be:
player1.name = "Jordan";
The dot expresses the relationship:
player1 → name
That is the code-level way of saying:
This field value belongs to this specific object.
this Can Appear on the Left SideInside the current object's instance-level code, you may see:
this.name
The left side:
this
means the current object.
The right side:
name
is the member being accessed.
The same dot rule applies.
Conceptually:
this.name
means:
the
namefield of the current object
This expression:
player1.name
identifies a member.
It does not change the member by itself.
A field changes when code performs an assignment such as:
player1.name = "Jordan";
Keep the operations separate:
member access
player1.name
field assignment
player1.name = "Jordan";
The next activity focuses directly on assignment.
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:
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.
C# distinguishes identifiers precisely.
If the field is:
jerseyNumber
then:
player1.jerseyNumber
and:
player1.jerseynumber
are not automatically the same identifier.
Preserve the member names used by the supplied project.
Suppose:
Player player1 = null;
Then:
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:
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.
Later in Module 1.1, you will encounter expressions that move through one object to another related object.
For now, focus on one step:
player1.name
or:
team1.name
Understand one object/member connection before following longer chains.
Whenever you see:
something.member
ask:
something refer to?The dot operator is small punctuation, but it is one of the most important ways C# expresses object-oriented structure.