0
0
C Sharp (C#)programming~10 mins

Reference assignment and shared state in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reference assignment and shared state
Create Object A
Assign Reference B = A
Modify Object via B
Check Object via A
Both A and B see same changes
When you assign one reference variable to another, both point to the same object. Changes through one reference affect the shared object.
Execution Sample
C Sharp (C#)
class Box {
  public int Value;
}
Box a = new Box();
a.Value = 5;
Box b = a;
b.Value = 10;
Console.WriteLine(a.Value);
This code shows two references pointing to the same object; changing the object via one reference affects the other.
Execution Table
StepActionVariable StatesOutput
1Create new Box object and assign to 'a'a -> Box(Value=0)
2Set a.Value = 5a -> Box(Value=5)
3Assign b = a (both reference same object)a -> Box(Value=5), b -> Box(Value=5)
4Set b.Value = 10 (modifies shared object)a -> Box(Value=10), b -> Box(Value=10)
5Print a.Valuea -> Box(Value=10), b -> Box(Value=10)10
💡 Execution ends after printing the shared object's Value, which is 10.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
anullBox(Value=5)Box(Value=5)Box(Value=10)Box(Value=10)
bnullnullBox(Value=5)Box(Value=10)Box(Value=10)
Key Moments - 2 Insights
Why does changing b.Value also change a.Value?
Because both 'a' and 'b' reference the same Box object (see execution_table step 3 and 4). Changing the object through 'b' affects what 'a' sees.
Does assigning b = a create a new object?
No, it only copies the reference. Both variables point to the same object (execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of a.Value?
A10
B5
C0
DUndefined
💡 Hint
Check the Variable States column at step 4 where both a and b have Value=10.
At which step do 'a' and 'b' start referencing the same object?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See the Variable States column; step 3 shows both a and b pointing to the same Box.
If we changed b.Value to 20 instead of 10 at step 4, what would a.Value be at the end?
A5
B20
C10
D0
💡 Hint
Since a and b share the same object, changing b.Value changes a.Value too (see variable_tracker).
Concept Snapshot
Reference assignment means two variables point to the same object.
Changing the object via one reference affects all references.
Assigning one reference to another copies the reference, not the object.
Use this to share and modify data without copying.
Remember: value types behave differently.
Full Transcript
This example shows how reference assignment works in C#. We create a Box object and assign it to variable 'a'. Then we assign 'b' to 'a', so both point to the same object. When we change the Value property through 'b', the change is visible through 'a' because they share the same object. The output prints 10, showing the shared state. This helps understand that reference variables hold addresses to objects, not copies.