1.1.22 Inspecting Nested Objects in the Debugger

The Debugger Can Show Relationships Between Runtime Objects

When a program is paused, debugger windows such as Autos can show more than simple values.

They can also show fields that refer to other objects.

Suppose the runtime relationship is:

Plain text
player1 → Player object
player1.team → Team object

Expanding player1 can reveal the nested team reference.

Expanding that Team object can reveal its fields.

This makes the object graph visible one relationship at a time.

Begin at a Meaningful Breakpoint

Suppose the source contains:

C#
Player player1 = new Player();
Team team1 = new Team();

player1.team = team1;
team1.name = "Wildcats";

Set a breakpoint after the relationship and value assignments when you want to inspect the completed state.

When execution pauses, locate player1 in the debugger.

Expand the Outer Object First

You may see something conceptually like:

Plain text
player1
  name = "Jordan"
  jerseyNumber = 7
  team = Team

The exact debugger display varies by Visual Studio version and supplied class structure.

The important part is that:

Plain text
team

now contains an object reference rather than:

Plain text
null

Expand the Nested Object

Expand:

Plain text
player1.team

You may then see:

Plain text
player1
  team
    name = "Wildcats"

This shows the relationship and the nested object's state at runtime.

The same information might be reached directly through:

Plain text
team1

if team1 is also visible in the current debugging context.

Compare the Nested State With UML

Suppose UML shows:

Plain text
player1 : Player  --------  team1 : Team
                            ----------------
                            name = "Wildcats"

Debugger evidence should support:

The debugger helps verify the runtime version of the modeled relationship.

Diagnose null by Expanding Step by Step

Suppose the source later executes:

C#
player1.team.name

and throws NullReferenceException.

Inspect:

Plain text
player1

If it exists, expand it.

Then inspect:

Plain text
team

If the debugger shows:

Plain text
team = null

stop there.

There is no nested Team object to expand.

You have found the broken step in the reference chain.

Compare Before and After Reference Assignment

Pause before:

C#
player1.team = team1;

You may observe:

Plain text
player1.team = null

Press F10.

Then inspect again:

Plain text
player1.team = Team object

Now expand that object.

This gives direct evidence that the reference assignment changed the Player's object relationship.

Keep Object Identity in Mind

When the debugger shows a nested Team object, ask:

Is this the Team object the UML intended?

The member having a non-null value is not enough by itself.

The program must connect the correct objects.

If the model contains team1, the reference assignment should correspond to that intended instance.

Do Not Confuse the Reference With the Nested Object's Fields

Conceptually:

Plain text
player1.team

is the reference to the Team object.

Inside that object:

Plain text
name = "Wildcats"

is part of the Team object's state.

These are different layers.

The outer Player stores a relationship.

The nested Team stores its own fields.

Use the Debugger as Runtime Evidence

A useful observation statement is:

After player1.team = team1 executed, the Player's team field was no longer null. Expanding the field showed the Team object, and its name field contained "Wildcats".

That is evidence tied to:

The Main Debugging Pattern

For nested objects:

Plain text
pause
  ↓
inspect outer object
  ↓
expand reference field
  ↓
confirm null or object
  ↓
expand nested object
  ↓
inspect nested fields
  ↓
compare with UML and requirement

This process prepares you to reason about more detailed object memory and reference behavior in the next batch without guessing from the source alone.