2.4.15 Inspecting List Items in the Debugger

Inspecting List Items in the Debugger

Pause the soccer application to compare the list’s current Count, indexed values, and object references with your prediction.

Explore the idea
Set a precise pause point

Use using System.Collections.Generic; at the top of the C# file, before its class definitions. Place the following statements inside an existing method.

Pause before the correction
List<int> roundGoals = new List<int>();
roundGoals.Add(2);
roundGoals.Add(0);
roundGoals.Add(3);
roundGoals[1] = 1;

In Visual Studio, click the margin beside roundGoals[1] = 1; to set a breakpoint. Start debugging and run the application action that reaches this method. When it pauses on that statement, the correction has not executed yet.

Inspect visible list state
  1. Open Debug > Windows > Locals while paused inside the method.
  2. Expand roundGoals and inspect Count and its current items. Depending on the debugger view, expand Results View to see indexed entries.
  3. Open Debug > Windows > Watch > Watch 1 and add roundGoals.Count and roundGoals[1]. Expect 3 and 0 before the correction.
  4. Use Step Over once to execute the assignment. The watched item becomes 1; Count stays 3.
Inspect references deliberately

These examples use the established Player class and its public integer field NumberOfTurns. For a Player list, expand an item and inspect NumberOfTurns. If two entries refer to the same Player, changing its field through either entry is visible through both. Equal-looking field values alone do not prove two references reach the same object.

Predict and check