LLearnCode
Academy/csharp/Model state deliberately

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.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

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.

  1. A positional record class supplies properties and generated value equality for its members.
  2. with creates a new record instance with selected changes, leaving the original top-level values alone.
  3. ReferenceEquals asks whether two references identify the same object; record == can report equal data for distinct instances.
  4. A mutable referenced collection can still be shared after with. Ordinary List equality is not automatic sequence equality.
Read the codecsharp · educational example
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);
  1. Copy with change

    changed is a new record with Score 20; original retains Score 10.

  2. Value equality

    original and equalData have equal member values, so == is true.

  3. 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 →