Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Structs vs classes: value semantics

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 44 of 85

A struct variable *is* the data; a class variable points at it. Copies, `ref`, and when each shape fits.

The copy rule

struct PointS { public double X, Y; }
class  PointC { public double X, Y; }

var s1 = new PointS { X = 1 };
var s2 = s1;  s2.X = 99;    // s1.X still 1 โ€” s2 is a COPY

var c1 = new PointC { X = 1 };
var c2 = c1;  c2.X = 99;    // c1.X is 99! c2 is the SAME object

A struct variable is the data (value type): assignment copies it. A class variable points at the data (reference type): assignment shares it. This is the single most consequential runtime difference in C#.

Choosing

  • Small, short-lived, data-like things where copying is harmless and identity is meaningless โ†’ struct (or better, readonly record struct).
  • Entities with identity and lifetime โ€” a bank account, a UI window โ†’ class.
  • Data passed around and compared by contents โ†’ record (a reference type with value semantics).

Beginner guidance: default to classes and records; reach for structs only when copying is genuinely what you want. Misusing big mutable structs causes mysterious copy-bugs โ€” the exact trap the practice below exercises.

โšก Now practice

Ready to Code
Data modeling workoutOrder statuses, shipments, points, and batches โ€” enums, records, and value semantics under discriminating tests.
4 challenges ยท ยท ~40 min