Recall & Review
beginner
What happens when you assign one reference type variable to another in C#?
Both variables point to the same object in memory, so changes through one variable affect the other.
Click to reveal answer
beginner
Explain shared state in the context of reference assignment.
Shared state means multiple variables refer to the same object, so modifying the object via any variable changes the state seen by all.
Click to reveal answer
beginner
Consider this code:<br><pre>class Box { public int Value; }<br>Box a = new Box();<br>a.Value = 5;<br>Box b = a;<br>b.Value = 10;</pre><br>What is the value of <code>a.Value</code> after this code runs?The value of
a.Value is 10 because a and b reference the same object.Click to reveal answer
intermediate
How can you avoid shared state when working with reference types?
Create a new object (a copy) instead of assigning the reference, so each variable points to a separate object.
Click to reveal answer
beginner
Why is understanding reference assignment important in programming?
Because it helps prevent bugs caused by unexpected changes to shared objects and helps manage how data is shared or isolated.
Click to reveal answer
What does assigning one reference variable to another do in C#?
✗ Incorrect
Assigning one reference variable to another makes both variables point to the same object in memory.
If you change a property of an object through one reference, what happens to other references to the same object?
✗ Incorrect
All references to the same object see the updated property because they share the same state.
How can you create a separate object to avoid shared state?
✗ Incorrect
Using 'new' creates a new object instance, so variables do not share the same state.
Which of these is a value type in C# that does NOT share state on assignment?
✗ Incorrect
int is a value type, so assignment copies the value, not the reference.
What is a common problem caused by shared state?
✗ Incorrect
Shared state can cause bugs because changes through one reference affect others unexpectedly.
Describe what happens when you assign one reference type variable to another in C# and how it relates to shared state.
Think about how variables point to objects in memory.
You got /4 concepts.
Explain how you can avoid shared state when working with objects in C#.
Consider how to make variables point to different objects.
You got /4 concepts.