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

Value types vs reference types mental model in C Sharp (C#) - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Value types vs reference types mental model
Declare value type variable
Store actual data in variable
Assign value type to another variable
Copy data to new variable
Change new variable value
Original variable unchanged
Declare reference type variable
Store reference (address) to object
Assign reference type to another variable
Copy reference (both point to same object)
Change object via new variable
Original variable sees changed object
Value types hold actual data copied on assignment; reference types hold a pointer to data, so assignments share the same object.
Execution Sample
C Sharp (C#)
int a = 5;
int b = a;
b = 10;

class Box { public int Value; }
Box x = new Box() { Value = 5 };
Box y = x;
y.Value = 10;
Shows how value types copy data and reference types share the same object.
Execution Table
StepVariableValueActionEffect
1a5Declare a and assign 5a holds 5
2b5Assign b = ab copies value 5
3b10Set b = 10b changes to 10, a unchanged
4xref#1Create Box object with Value=5, assign to xx points to object with Value=5
5yref#1Assign y = xy points to same object as x
6y.Value10Set y.Value = 10Object's Value changes to 10
7x.Value10Check x.Valuex sees updated Value 10
💡 Execution ends after showing value and reference type behaviors.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
aundefined55555
bundefined510101010
xundefinedundefinedundefinedref#1ref#1ref#1
yundefinedundefinedundefinedref#1ref#1ref#1
Object.Value (x/y)undefinedundefinedundefined51010
Key Moments - 3 Insights
Why does changing b not affect a?
Because b is a value type copy of a (see execution_table step 3), changing b only changes its own copy.
Why does changing y.Value also change x.Value?
Because x and y both reference the same object (see execution_table step 5), so changing the object via y affects what x sees.
Is the data stored inside the variable for reference types?
No, the variable stores a reference (address) to the object, not the actual data (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of variable a?
A5
B10
Cundefined
Dref#1
💡 Hint
Check variable 'a' value in execution_table row for step 3.
At which step do variables x and y start pointing to the same object?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the action and effect columns in execution_table rows for steps 4 and 5.
If we changed y.Value to 20 instead of 10 at step 6, what would x.Value be after?
A5
B20
C10
Dundefined
💡 Hint
Since x and y reference the same object, changing y.Value changes x.Value (see variable_tracker).
Concept Snapshot
Value types store actual data; copying duplicates data.
Reference types store address to data; copying duplicates reference.
Changing a copied value type does not affect original.
Changing data via one reference affects all references.
Examples: int is value type; class instances are reference types.
Full Transcript
This visual execution shows how value types and reference types behave differently in C#. Value types like int store the actual data in the variable. When you assign one value type variable to another, it copies the data. Changing the new variable does not affect the original. Reference types like class instances store a reference (address) to the object. Assigning one reference variable to another copies the reference, so both variables point to the same object. Changing the object through one variable is seen by the other. The execution table traces these steps with variables a, b (value types) and x, y (reference types). The variable tracker shows how values change after each step. Key moments clarify common confusions about copying and shared data. The quiz tests understanding of these behaviors. This mental model helps beginners grasp how data is stored and shared in C#.