intermediate · 24 minute study
Records describe values, but with is a shallow copy
Choose deliberately between comparing identity and comparing a data value. Avoid assuming convenient record syntax makes nested data immutable.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- A class can protect a rule, not just hold fields — A property that anyone can overwrite cannot protect an inventory rule. Put state changes behind behavior that checks the rule once.
First, build a mental model
A record supplies value-oriented equality and convenient copying. A record class is still a reference type; its generated equality and a with expression do not recursively redesign the members you put inside it.
- A positional record class supplies properties and generated value equality for its members.
- with creates a new record instance with selected changes, leaving the original top-level values alone.
- ReferenceEquals asks whether two references identify the same object; record == can report equal data for distinct instances.
- A mutable referenced collection can still be shared after with. Ordinary List equality is not automatic sequence equality.
var original = new Player("Ada", 10);
var changed = original with { Score = 20 };
var equalData = new Player("Ada", 10);
Console.WriteLine(original.Score);
Console.WriteLine(changed.Score);
Console.WriteLine(original == equalData);
Console.WriteLine(ReferenceEquals(original, equalData));
record Player(string Name, int Score);- Copy with change
changed is a new record with Score 20; original retains Score 10.
- Value equality
original and equalData have equal member values, so == is true.
- Identity
They are different instances, so ReferenceEquals is false.
Production thinking
Take this beyond the example.
Use records for snapshots and messages where value semantics are intentional. Avoid accidental mutable keys in dictionaries and decide which fields belong in equality.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Depend on the behavior you need →