Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Value semantics vs reference semantics

โญโญโญ advancedโณ 13 min read๐Ÿ“ Lesson 157 of 265

Copy behavior, assignment, equality, and why two structs are never the same object.

Value semantics vs reference semantics

A value type variable is the data; a reference type variable is an address to the data. Everything else follows:

var a = new Counter();   // struct
var b = a;               // full copy โ€” two independent objects
b.Increment();
// a unchanged

var s1 = new Service();  // class
var s2 = s1;             // address copy โ€” ONE object, two names
s2.Tick();
// s1 observed the tick

The trap is not the definition โ€” it is the blast radius. Copies stay local: mutation can't leak. References spread: every alias can observe every mutation, which is why shared mutable references are the root cause of most "impossible state" bugs. Equality follows the same split: value types compare contents (a.Equals(b)), reference types compare identity unless the author overrode Equals.

Decision rule for modeling: default to immutable value or record when the thing is data (Money, Coordinates); use a class when the thing has identity that outlives its values (UserSession, OrderPipeline) โ€” identity, not size, is the real axis.

โšก Now practice

Ready to Code
Semantics under the microscopePredict-and-verify exercises: copies, boxes, null-state, and resolution traps.
4 challenges ยท ยท ~40 min