As class designs become larger, you may encounter a field whose name looks the same as the class used for its type.
For example, inside a different class:
public Team Team;The two occurrences of Team do not play the same role.
Read the declaration from left to right:
public Team Team;Conceptually:
public Team Team
↑ ↑
type field nameThe first Team identifies the custom type.
The second Team identifies the field stored on the current object.
Suppose a Player class contains:
public Team Team;The declaration means:
The field is not the class itself.
It is a member whose declared type happens to have the same spelling as the member name.
Consider:
Team team1 = new Team();
player1.Team = team1;There are several uses of the word Team:
The surrounding syntax tells you which role the name is playing.
When you see:
player1.Teamthe expression means:
The dot operator makes the object-member relationship visible.
C# is case-sensitive.
That means:
Team
teamare different identifiers.
Many codebases use naming conventions that make types and fields visually different, such as:
public Team team;But when existing source or a required design uses:
public Team Team;you still need to be able to read it correctly.
Do not rely on capitalization alone. Use the declaration structure and surrounding syntax.
There is an important boundary.
A field inside a different class can have the same spelling as another class type.
For example, this can describe a relationship from Player to Team:
public class Player
{
public Team Team;
}But a member cannot use the same name as its own enclosing class in a way that conflicts with the class declaration itself.
When reading unfamiliar code, first identify the enclosing class, then identify each member inside it.
Instead of seeing:
Team Teamand assuming the code is duplicated, read the roles:
type field-nameThis same habit helps with method parameters and local variables later: names can look similar while playing different roles depending on where they are declared and how they are used.
When a class name and a field name look the same, determine meaning from position and context:
public Team Team;
↑ ↑
type fieldThe class type describes what kind of object may be referenced. The field name identifies where that reference is stored on the current object.