Module 4 · Reliable programs: copy, handle errors and check results · Lesson 13 of 20
Trace copies: separate values and shared objects
You edit a draft task, but the saved task changes too. How many objects did you actually create? Draw the references before trying to fix the result.
The program below puts an int copy beside a task-reference copy. Track them separately: changing a number, changing an object, and redirecting a variable are three different actions.
Predict before drawing
The first two variables are ints. The next two are StudyTask variables. Write the two output lines, then run this complete program.
using System;
int planned = 10;
int displayed = planned;
displayed = 15;
StudyTask saved = new StudyTask("Read C#");
StudyTask draft = saved;
draft.IsDone = true;
draft = new StudyTask("Practise");
Console.WriteLine($"Minutes: {planned}/{displayed}");
Console.WriteLine($"Done: {saved.IsDone}/{draft.IsDone}");
class StudyTask
{
public string Title { get; set; }
public bool IsDone { get; set; }
public StudyTask(string title)
{
Title = title;
IsDone = false;
}
}What are the two output lines?
Show answer
Correct answer: C
The int copy is independent. The task assignment first shares an object, so its IsDone becomes true. Reassigning draft then points draft at a new unfinished task.
Option A: Changing displayed does not change planned.
Option B: Before the reassignment, draft.IsDone changed the shared original task.
Option D: Both later assignments do take effect.
Hint: Track the mutation before moving the draft reference.
One copy rule, different values
Ordinary assignment copies the value on its right. For an int variable, that value is a number. For a class variable, it is a reference to an object. Copying that reference gives another route to the same object; it does not create another StudyTask.
After reference copying, both variables reach Task A. The property assignment changes the one object, so saved also observes IsDone true. The later new expression creates Task B and the assignment redirects only draft. Final printed task state is True/False. Separately, the int variables retain independent 10 and 15 values.
These boxes and arrows describe values and object identity. They are not claims about stack addresses or where a runtime must place storage. For this problem, ask which object a reference reaches, rather than memorising a storage slogan.
Before draft is reassigned, which line changes the task also reached by saved?
Show answer
Correct answer: A
Assigning a property changes the object reached by draft. While both references reach Task A, saved observes that state too.
Option B: This redirects draft to another object; it does not redirect saved.
Option C: This copies an int value and does not touch a task.
Option D: This changes only the int variable displayed.
Hint: Separate changing an object from changing a variable.
Your turn: make an independent edit
Create a saved task with IsDone false. Create a draft that starts with the same Title but is a different task. Mark only the draft done. Predict the saved and draft states before running your attempt. Compare with this complete solution.
using System;
StudyTask saved = new StudyTask("Read C#");
StudyTask draft = new StudyTask(saved.Title);
draft.IsDone = true;
Console.WriteLine($"Saved: {saved.IsDone}");
Console.WriteLine($"Draft: {draft.IsDone}");
Console.WriteLine($"Same object: {ReferenceEquals(saved, draft)}");
class StudyTask
{
public string Title { get; set; }
public bool IsDone { get; set; }
public StudyTask(string title)
{
Title = title;
IsDone = false;
}
}Expect False for saved, True for draft, and False for the same-object check. ReferenceEquals is a method that asks whether these two references identify the same object. Here a second new expression creates a second task.
Why is the second draft independent in this small model?
Show answer
Correct answer: D
The explicit new expression creates a separate task whose properties can change independently.
Option A: Names do not determine whether references share an object.
Option B: Ordinary class-variable assignment copies a reference.
Option C: This bool property has a setter and can change.
This copy is sufficient for the shown model: its Title is a string with immutable character contents, and IsDone is a bool. If you later add a mutable list inside StudyTask, copying the outer object alone may still share that list. Revisit which data the editor is allowed to change independently.
Explain it back
Use your drawing to explain the difference between assigning true to draft.IsDone and assigning a new object to draft. Then change the initial numbers and task titles and predict again. Understanding should transfer to changed values.
References: Microsoft - value types and copying and classes and shared references.
Quick reference
- Assignment copies the expression value.
- An int copy is an independent number.
- A class-variable copy is another reference to the same object.
- Mutation changes the reached object.
- Reassignment changes the value stored in one variable.
- A new outer object may still share nested mutable data.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·