You have already defined fields whose type is another class.
For example:
class Player
{
public Team team;
}A class can also contain a field whose type is the same class.
For example:
class Player
{
public string name;
public Player teammate;
}The teammate field can refer to another Player object.
In:
public Player teammate;the field type is:
PlayerThe field name is:
teammateRead it as:
Suppose:
Player player1 = new Player();
Player player2 = new Player();Then:
player1.teammate = player2;connects player1 to the existing player2 object.
Conceptually:
player1 : Player
|
| teammate
v
player2 : PlayerThe assignment stores an object reference.
It does not copy all of player2 into player1.
If no Player has been assigned yet, the field may contain:
nullFor a newly created Player:
Player player1 = new Player();the state may include:
teammate = nulluntil the relationship is assigned.
After creating two Player objects:
Player player1 = new Player();
Player player2 = new Player();you can connect them:
player1.teammate = player2;Now:
player1.teammatereaches a Player object.
Nested access can continue from there:
player1.teammate.namewhen the reference is not null.
Suppose:
player1.name = "Jordan";
player2.name = "Casey";
player1.teammate = player2;Then:
player1.name = "Jordan"
player1.teammate.name = "Casey"The relationship does not merge the two Player objects.
Each keeps its own fields and identity.
In a soccer context, the same class might represent:
For example:
class Team
{
public string name;
public Team rival;
}The usefulness depends on the domain model.
Do not add a self-typed field merely because the syntax is available.
The relationship should come from the current design.
C# can technically allow a reference such as:
player1.teammate = player1;But whether that relationship makes sense depends on the model.
For a teammate relationship, it would usually not represent the intended soccer meaning.
Use the requirement and UML to determine which object the field should reference.
A class diagram might show:
Player
-------------------------
name : string
teammate : PlayerAn object diagram might show:
player1 : Player -------- player2 : PlayerThe C# field:
public Player teammate;makes the same-type relationship possible.
The runtime assignment:
player1.teammate = player2;establishes the relationship between the specific objects.
A custom class type can appear as a field type even inside its own class.
That allows an object to refer to another object of the same kind:
The field represents a relationship, not a duplicate copy of the object.