void Method Performs Behavior Without Returning a ValueA method declaration such as:
public void DisplayName()
{
System.Console.WriteLine(this.name);
}
uses the return type:
void
That tells C# the method does not return a value to the calling code.
If:
team1.DisplayName();
executes, the call asks the Team object to perform the behavior.
After the method finishes, execution continues with the next statement after the call.
For example:
public void ResetScore()
{
this.score = 0;
}
The method returns no value.
It still changes the current Team object's state.
So:
void
does not mean:
the method does nothing
It means:
the method does not send a result value back to the caller.
Parameters are introduced in Module 1.4.
For now, use methods such as:
ResetScore()
DisplayName()
StartMatch()
with empty parentheses.
void When the Current Purpose Is an ActionIf the method's job is:
Reset the score.
a void method is a natural introductory design.
Methods that calculate and return a value are taught later.
A sequence diagram can show one object calling a void method on another object.
That visual model is the focus of CO-035.