liveBook · Manning

Pass by value

As explained in High Performance Unity Game Development

A parameter-passing behavior in which a function receives a copy of an argument's value.

A player picks up 10 XP, but the displayed total stays at zero. In C#, a function argument is passed by value unless specified otherwise: the function receives a copy. If the argument is a struct, that copy contains the struct’s data, so changing it does not change the caller’s original. section 2.3.1 section 2.3

Listing 2.8 Example of adding xp to a copy of our game data struct
GameDataStruct myGameDataStruct;
myGameDataStruct.XP = 0;
 
public void AddXP(GameDataStruct gameDataStruct, int xp)
{
    gameDataStruct.XP += xp; #A
}

AddXP(myGameDataStruct, 10);

The XP example passes a game-data struct to AddXP. The function increments its local copy, leaving the original XP unchanged. section 2.3.1

For a class, array, or string, call by value copies the reference, not the object’s contents. Both references still point to the same object. This is why changing data through a class parameter can affect the caller’s object even though the parameter was passed by value. section 2.3 section 2.4.1

When a function must update a struct owned by its caller:

  • Pass it with C# ref to modify the original, as in an XP update or a player-position change. section 2.3.1 section 2.4.1
  • Alternatively, return the changed value and assign it back. Passing the whole game-data class also permits mutation, but its function signature reveals less about which data may change. section 2.4.1

Copying also has a cost: larger structs require more data to be copied. A Unity Editor test on a MacBook Pro M3 found a marked increase between 16- and 20-byte structs, but its IL2CPP tests found no measurable size-related difference. Measure on the target runtime rather than treating one threshold as universal. A struct remains passed by value even when it is a field of a heap-allocated class. section 2.3.2 section 2.4.1

Table 2.1 Passing structs by value performance
Struct size
Time to pass by value
(Macbook Pro M3)
% difference from 4 bytes
% difference for 4 additional bytes
4 bytes
0.2373sec
0%
0%
8 bytes
0.245sec
3.14%
3.14%
12 bytes
0.2617sec
9.32%
6.38%
16 bytes
0.2754sec
13.83%
4.97%
20 bytes
0.3314sec
28.39%
16.9%
24 bytes
0.3415sec
30.51%
2.96%

Compare the measured cost of passing structs of different sizes; the table illustrates an Editor result, not a guarantee for every device or build. section 2.3.2

How High Performance Unity Game Development covers it

How deep it goes
Explains copied values and references, works through ref and return-and-assign fixes, and qualifies struct-size measurements by runtime.
Examples
Demonstrates lost XP in a copied game-data struct and alternative ways to update a player-position struct.
What it assumes
Assumes basic C# function and type syntax while explaining what gets copied for structs and reference types.
What you can do after
Diagnose/debug with it by spotting copy-only updates, then use C# ref or return-and-assign when the caller needs the changed struct.
How it connects
Connects struct and class semantics to reference types, stack and heap storage, and data locality.

Also explained in

Compare how 4 books explain pass by value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.