Value types (int, struct, enum): the value is copied on assignment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // ====================================================================== // Q: Value types vs reference types. class vs struct vs record? // // - Value types (int, struct, enum): the value is copied on assignment. They // live inline (on the stack or inside the object that contains them). // - Reference types (class, string, arrays): the variable holds a reference to // an object on the heap, which the GC cleans up. // - struct: for small, immutable values (e.g. readonly struct Point). class: for // objects with identity and behavior. record: a reference type with value // equality, with-expressions and immutability. Ideal for DTOs and value // objects. There is also record struct. // ====================================================================== public record Money(decimal Amount, string Currency); var a = new Money(10, "EUR"); var b = a with { Amount = 20 }; // new object, a does not change Console.WriteLine(a == new Money(10, "EUR")); // True: compares values, not references |