C# uses named storage in several contexts.
Two important categories are:
They can both hold values.
They do not represent the same kind of program state.
Consider:
class Player
{
public int jerseyNumber;
}The field jerseyNumber is part of the Player object's structure.
After:
Player player1 = new Player();that Player object has its own jerseyNumber field.
Another Player object has its own field:
Player player2 = new Player();Conceptually:
The field is part of each object's state.
Consider:
public void SetJerseyNumber(int number)
{
this.jerseyNumber = number;
}The parameter number is a variable available during that method call.
It receives the caller's argument.
It is not a permanent field on the Player object.
This line:
this.jerseyNumber = number;contains both ideas.
this.jerseyNumberbelongs to the current Player object.
numberbelongs to the current method call.
The assignment copies the incoming value into the object's state.
Suppose:
player1.SetJerseyNumber(7);During the call:
number = 7The method assigns:
this.jerseyNumber = number;After the method finishes, the parameter number is no longer available as that active method parameter.
But the Player field can still contain:
jerseyNumber = 7The field is part of the object's continuing state.
C# methods can declare local variables.
A local variable belongs to the method or block scope where it is declared.
Later Learning Activities show how to declare, initialize, and use local variables in calculations and decisions.
For this page, the key distinction is:
Field
Parameters are one kind of method-level variable.
Suppose a method needs a value only while performing one task.
That value does not automatically need to become permanent object state.
Fields should represent information the object needs to keep as part of its design.
Method-level variables support the method's current work.
If the Player is required to remember its jersey number after SetJerseyNumber finishes, storing the value only in the parameter is not enough.
The method needs to transfer that value into the field:
this.jerseyNumber = number;When reading unfamiliar source, ask:
It may be a field.
It is a parameter.
It is a local variable.
The declaration location helps reveal the role and scope.
While paused inside a method, debugger windows can show:
That lets you distinguish:
object statefrom:
method execution stateCO-038 establishes this information flow:
Later Module 1.4 activities add UML parameter notation, comparisons, null checks, multiple parameters, local variables, and more complex conditional logic.