liveBook · Manning

List

As explained in High Performance Unity Game Development

A C# List is a dynamically sized sequence collection that stores elements in an internal array and provides indexed access and element operations.

A survival game may need to add and remove enemies as play continues. C# List makes that convenient: it grows dynamically, provides indexed access, and offers insertion and removal operations. Its elements live in an internal array, so a List of primitive values retains contiguous storage. It is not a Linked List, whose separate nodes can be scattered through memory. section 6.1 section 6.1.1

That convenience has costs worth checking in a frequently updated game loop. When a List runs out of capacity, it allocates an array twice as large, copies its elements, and leaves the old array for garbage collection. Supplying the maximum expected capacity avoids those resizing costs, but does not make every List operation cheap. section 6.1.2

Choose operations with their data movement in mind:

  • RemoveAt() shifts later elements over the removed position; Insert() shifts them to make space. An insertion that exceeds capacity also resizes the internal array. section 6.1.3
  • For a known maximum enemy count, a fixed array plus a separate count can track how many entries are relevant. Packing entries after removal still requires moving data; it does not make removal free. section 6.8
  • Measure a List against an array for the actual workload. In an increment test across the devices tested, the array was approximately 6–8 times faster; the precise cause of the access difference was not established. section 6.1.4
Figure 6.6 Results of incrementing an array and a List of 10,000 elements 10,000 times. The array was 6-8x faster than List.

The benchmark figure compares repeated increments of 10,000 List and array elements. It illustrates why contiguous internal storage alone does not guarantee equal indexed-access performance. section 6.1.4

Use List when its flexible operations are useful and their measured cost is acceptable. For a bounded, frequently traversed enemy set, consider an array and explicit count instead; that trades built-in convenience for more visible allocation and movement costs. section 6.0 section 6.8

How High Performance Unity Game Development covers it

How deep it goes
Explains resizing and element shifting, then benchmarks indexed access without establishing the precise cause of its slowdown.
Examples
Uses changing enemy data, Count-and-Capacity growth, and a Unity increment benchmark comparing List with an array.
What it assumes
Assumes familiarity with C# arrays and indexed loops while distinguishing List from Linked List explicitly.
What you can do after
Evaluate/understand List costs and choose between preallocated List storage and an array with an explicit count.
How it connects
Contrasts List with Linked List and fixed arrays through data locality, garbage collection, and packed-array counts.

Also explained in

Compare how 23 books explain list →

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.