Records and with expressions
Records and with expressions
A record is a class or struct with compiler-generated members designed for value-based comparison and concise data modeling. record and record class declare reference types; record struct declares a value type. Positional record syntax generates properties from constructor parameters. For a positional record class these properties are normally init-only; a plain positional record struct has read-write properties. readonly record struct is useful when the value should not be changed after initialization.
A with expression constructs a copy with selected members changed. For the ordinary generated copy behavior it is shallow: a field holding a reference is copied as a reference. Use immutable members or explicitly copy nested mutable data when independent state is needed.
using System;var first = new Parcel("A7", 2);var changed = first with { Weight = 4 };Console.WriteLine(first == new Parcel("A7", 2));Console.WriteLine(first.Weight);Console.WriteLine(changed.Weight);Console.WriteLine(object.ReferenceEquals(first, changed));record Parcel(string Code, int Weight);The two independently created records with equal values compare equal. With creates another record object and applies the new Weight, leaving the original property unchanged.
Expected output (reasoned, not executed):
True24FalseBuild on the first example
using System;using System.Collections.Generic;var first = new Order("A", new List<string> { "pen" });var second = first with { Code = "B" };second.Items.Add("pad");var independent = first with { Items = new List<string>(first.Items) };independent.Items.Add("tape");Console.WriteLine(first.Items.Count);Console.WriteLine(independent.Items.Count);record Order(string Code, List<string> Items);The first two records share a list; constructing a new list for the third separates the collection. Its elements are immutable strings. For mutable element objects a list copy alone would still share them.
Expected output (reasoned, not executed):
23Apply the idea to another case
using System;var start = new GridCell(2, 4);var next = start with { Column = 5 };Console.WriteLine(start.Column);Console.WriteLine(next.Column);Console.WriteLine(start == new GridCell(2, 4));readonly record struct GridCell(int Row, int Column);The record struct has value copy semantics and generated equality. With changes the new value rather than the original.
Expected output (reasoned, not executed):
45TrueCommon mistakes and design choices
Record equality is not automatic recursive comparison of collections. Two lists with equal elements normally compare by reference with their default equality. Records are not inherently deeply immutable, and with can bypass assumptions that a constructor alone establishes. Validate invariants wherever changes are allowed. A primary constructor on a normal class does not automatically generate public positional properties as record syntax does.
Practice
Define a readonly record struct Temperature(int Degrees, string Unit). Start at (20, "C"), create a copy with Degrees = 22 and compare both against a new (20, "C"). Explain whether any shared mutable state exists.
Use the manual practice task and knowledge check to verify your understanding. All examples target C# 14 / .NET 10; execution and ByteMind renderer support remain unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Records and with expressions — knowledge check
Apply the lesson concepts to the following scenarios.
What kind of type does record Parcel(...) declare?
What is copied by a normal generated with operation on a list-valued property?
Does a record automatically compare separate lists element by element?
Which declaration creates a value type with generated record equality?
Does a primary constructor on an ordinary class automatically create public positional properties?