You have seen types such as:
Player
and object instances such as:
player1 : Player
A type describes what kind of thing an object can be.
The running program still needs an actual object instance.
In C#, the new operator is used to create that instance.
A basic example is:
Player player1 = new Player();
The most important part for this activity is:
new Player()
That expression creates a new Player object.
This statement can feel dense at first:
Player player1 = new Player();
Break it into parts.
new Player()Create a new object whose type is Player.
player1Use the name player1 to refer to that object.
PlayerThe variable/reference is intended to work with a Player object.
At an introductory level, you can read the whole statement as:
Create a new Player object and let
player1refer to it.
new Creates a Separate ObjectConsider:
Player player1 = new Player();
Player player2 = new Player();
There are two new Player() expressions.
That means the program creates two separate Player objects.
Even before values are assigned, player1 and player2 can refer to different object instances.
Now assign names:
player1.name = "Jordan";
player2.name = "Casey";
The two objects can hold different state because they are separate instances.
new Is Not the Same as Reusing an ObjectSuppose you already have:
Player player1 = new Player();
player1.name = "Jordan";
If you later write:
player1 = new Player();
you are not “refreshing” the same object.
You are creating another Player object and changing what player1 refers to.
The state that belonged to the earlier object does not automatically appear in the new one.
This matters when you are translating a UML object snapshot.
If the UML contains one object instance, you normally want one corresponding new operation for that object in the introductory setup.
new Creates the Object Before Its Values Are Filled InWith the simple classes used at this stage, object creation and field assignment can happen as separate steps.
For example:
Player player1 = new Player();
player1.name = "Jordan";
player1.jerseyNumber = 7;
player1.isAvailable = true;
The first statement creates the object.
The later statements establish the particular soccer state you want that object to represent.
This connects directly to UML:
player1 : Player
-------------------------
name = "Jordan"
jerseyNumber = 7
isAvailable = true
newA useful place to pause is immediately after:
Player player1 = new Player();
At that moment, the object exists.
The later field assignments may not have executed yet.
If you inspect player1, you can observe the new object before all of the scenario-specific values have been assigned.
Then, after the assignment statements execute, you can inspect the same object again and see its changed state.
This makes new more than a vocabulary term.
You can observe the effect at runtime.
new Works With the Type Being CreatedIf the program also has a Team type, you might see:
Team team1 = new Team();
The pattern is the same:
Type name = new Type();
For a player:
Player player1 = new Player();
For a team:
Team team1 = new Team();
For a match:
Match match1 = new Match();
The exact types available in a supplied course project come from that project.
Do not invent a class simply because its name would be convenient.
Creating two objects does not automatically connect them.
For example:
Player player1 = new Player();
Team team1 = new Team();
creates a Player object and a Team object.
The fact that both exist does not automatically mean the player belongs to that team.
A relationship requires the program to store an appropriate reference between the objects.
That idea becomes more important later.
For now, keep the distinction clear:
new creates an object.
A later assignment can give it values or connect it to another object.
new Back to the ModelWhenever you see:
new Player()
ask yourself:
Which Player object is this supposed to represent?
If the requirement or UML shows:
player1 : Player
then the C# object creation should support that intended instance.
That connection keeps the code grounded in the model instead of becoming a collection of unexplained statements.