A survival game needs to update many enemies. One approach is to keep their positions, directions, and velocities in separate arrays held by a single class. The class groups long-lived game data; the arrays let each update loop work through the data it needs. section 2.1 section 2.7
Listing 2.1 C# class holding all the data for all enemies
public class EnemyData
{
public Vector2[] Position;
public Vector2[] Direction;
public float[] Velocity;
}The class holds three arrays rather than one separate object for every enemy. section 2.1
Choose a class for data containing arrays or strings, data whose fields are not always used together, or data too large to make a convenient small value. A small struct is a better fit when its fixed-size fields are used together. An array of structs stores its elements contiguously; an array of classes holds references to objects that may be spread across the heap. That difference can affect cache misses during iteration. section 2.1 section 2.8
Passing a class object to a function copies its reference, not the whole object. The function can therefore change the original object's data. This is convenient, but a parameter typed as a broad game-data class does not reveal which fields the function might modify. A struct field inside that class behaves differently when passed on its own: passing it by value copies the field, so changes to the parameter do not update the original. Pass it with `ref`, or return and assign the changed value, when the original must change. section 2.4.1
Listing 2.16 Correct example of passing a class
public void MovePlayerHorizontally(GameData gameData, float amount)
{
gameData.PlayerPosition.x += amount;
}
GameData gameData = new GameData();
MovePlayerHorizontally(gameData, 10.0f);Passing the game-data class lets the movement function update its player-position field through the shared object. section 2.4.1


























