A class can contain more than one method.
One method may call another method that belongs to the same current object.
For example:
class Team
{
public string name;
public void DisplayName()
{
System.Console.WriteLine(this.name);
}
public void ShowTeam()
{
this.DisplayName();
}
}
Inside ShowTeam, the call:
this.DisplayName();
asks the current Team object to run its own DisplayName method.
this Keeps the Receiver ExplicitSuppose:
Team team1 = new Team();
team1.name = "Wildcats";
Then:
team1.ShowTeam();
starts execution in ShowTeam.
Inside that method:
this.DisplayName();
the word:
this
still refers to team1.
Conceptually:
team1.ShowTeam()
↓
this is team1
↓
this.DisplayName()
↓
DisplayName runs on team1
The call stays within the same object.
this.DisplayName();
The current object receives the call.
this.player.DisplayName();
The code follows a reference to another object, and that other object receives the call.
The final receiver changes the execution context.
A same-object call can be represented as a message that returns to the same lifeline.
Conceptually:
team1 : Team
|
| ShowTeam()
|
|── DisplayName() ──┐
|<──────────────────┘
The important meaning is:
While processing one Team method, the same Team object calls another of its methods.
Even though the object remains the same, C# still enters another method.
Execution has a new method frame.
Later debugger tools such as the Call Stack make this visible.
Suppose:
public void ShowTeam()
{
this.DisplayName();
this.DisplayScore();
}
ShowTeam coordinates two focused behaviors.
This is useful when each method has a clear responsibility.
Do not turn one method into a random chain of calls merely because calling methods is available.
The calls should support the method's purpose.
If:
this.name
belongs to the current Team object, both:
ShowTeam()
and:
DisplayName()
can work with that same object's state when their design requires it.
The current object remains the same through the same-object call.
When you see:
this.DisplayName();
read:
Ask the current object to perform its
DisplayNamebehavior.
This wording keeps the method attached to the object that owns it.
Consider:
team1.ShowTeam();
which calls:
this.DisplayName();
You now have one method call inside another.
That creates a nested execution path.
The next Learning Activity focuses on tracing that path carefully.