int a = 5; int b = a; b = 10; Console.WriteLine(a);
In C#, int is a value type. When b is assigned a, it copies the value. Changing b later does not affect a.
class Box { public int Size; } Box box1 = new Box() { Size = 3 }; Box box2 = box1; box2.Size = 7; Console.WriteLine(box1.Size);
Box is a class, so box1 and box2 refer to the same object. Changing box2.Size changes box1.Size as well.
struct Point { public int X; public int Y; }
Point p1 = new Point() { X = 1, Y = 2 };
Point p2 = p1;
p2.X = 5;
Console.WriteLine(p1.X);Point is a struct (value type). Assigning p2 = p1 copies the values. Changing p2.X does not affect p1.X.
class Container { public int Value; } void Modify(Container c) { c.Value = 10; c = new Container() { Value = 20 }; } Container cont = new Container() { Value = 5 }; Modify(cont); Console.WriteLine(cont.Value);
The method modifies the Value property of the object cont points to, setting it to 10. Then it reassigns the parameter c to a new object, but this does not change cont outside the method.
In C#, value types (like int, struct) are usually stored on the stack, which is fast and short-lived. Reference types (like class) are stored on the heap, and variables hold references (pointers) to these objects.
