When should you define a struct instead of a class?
Short answer
Use a struct for small, immutable, value-like data (under ~16 bytes) that is copied cheaply and has value semantics; otherwise use a class. Mutable structs are error-prone.
Advertisement
AModel answer
Use a struct for small, immutable, value-like data (under ~16 bytes) that is copied cheaply and has value semantics; otherwise use a class. Mutable structs are error-prone.
public readonly struct Money(decimal Amount, string Currency);Real-world example
Coordinates, money, or a date range are natural structs; an entity with identity is a class.