1.7.3 Reassigning an Object Parameter

A Parameter Is a Separate Variable

For an ordinary C# method parameter, the reference value is copied into the parameter. The caller's variable and the parameter start as separate paths to the same object.

Caller variable and parameter initially lead to the original Player.
Initial relationship
  • New
  • Changed
  • Removed
Reassignment Redirects the Parameter

Inside a method, this statement creates a Player and redirects `selected`:

selected = new Player();

The caller's variable has not been assigned a new reference. It still leads to the original Player.

Changing the New Player Does Not Change the Old One

If `selected.Name` then becomes Casey, the new Player has that name. The original Player may still have the name Jordan. The change follows the parameter's new destination.

After parameter reassignment caller still reaches Jordan and parameter reaches Casey.
How the relationship changes
  • New
  • Changed
  • Removed
Field Mutation Has a Different Effect

This statement does not redirect the parameter:

selected.Name = "Casey";

If `selected` still refers to the original Player, its Name changes and the caller sees Casey through `original`.

Ordinary Parameters Are Passed by Value

The reference value is copied into an ordinary parameter. This is why a method can change a shared object's fields but cannot redirect the caller's variable by assigning its own parameter. C# `ref` parameters follow different rules and are a separate feature.