Records are data-focused types with generated value equality, readable `ToString`, and nondestructive mutation through `with`. An `init` accessor permits assignment during construction or object initialization but prevents normal reassignment afterward. These features reduce accidental mutation in DTOs, commands, configuration, and domain value objects. ```csharp public sealed record CreateOrder( Guid CustomerId, IReadOnlyList<OrderLine> Lines); public sealed record OrderLine { public required string Sku { get; init; } public required int Quantity { get; init; } public decimal UnitPrice { get; init; } } var original = new OrderLine { Sku = "COFFEE", Quantity = 1, UnitPrice = 120m }; var corrected = original with { Quantity = 2 }; ``` Immutability is not automatic all the way down: a record containing a mutable `List<T>` can still be changed, and record equality compares that list reference rather than each element. Prefer immutable collections or expose read-only abstractions, validate at the boundary, and use classes when identity and a controlled mutable lifecycle matter more than value equality.
Prince Yadav · · 0 likes