| Value type | Reference type |
|---|---|
| Stores the data itself | Stores a reference (address) to the data |
| int, double, bool, char, struct, enum | class, string, array, interface, delegate |
| Usually on the stack | Object lives on the heap |
| Assignment copies the value | Assignment copies the reference (both point to the same object) |
Copying a value type vs copying a reference
int a = 10;
int b = a;
b = 20;
Console.WriteLine(a); // 10
var p1 = new Person { Name = "Rahim" };
var p2 = p1;
p2.Name = "Karim";
Console.WriteLine(p1.Name); // Karim
Boxing = value type → object (heap). Unboxing = object → value type. string is a reference type but immutable.