Select Page

Two records can look identical in the debugger and still compare unequal. The useful question is what each stored field considers equal. This guide shows three failures to watch for when comparing snapshots, copying options or writing tests.

Examples compiled and ran with .NET SDK 10.0.401, net10.0 and C# 14, with nullable analysis and implicit usings enabled. Each example is a separate console program; statements precede type declarations. The features used here need no C# 14 syntax.

Arrays keep their own equality rules

An array stored inside a record compares by reference. Two separately allocated arrays with identical elements make these baskets unequal. Sharing one array makes them equal.

var first = new Basket("desk", new[] { "pen" });
var separate = new Basket("desk", new[] { "pen" });
var same = new Basket("desk", first.Items);
Console.WriteLine(first == separate);
Console.WriteLine(first == same);
var copy = first with { Name = "travel" };
copy.Items[0] = "pencil";
Console.WriteLine(first.Items[0]);
var independent = first with { Items = (string[])first.Items.Clone() };
independent.Items[0] = "eraser";
Console.WriteLine(first.Items[0]);
public record Basket(string Name, string[] Items);

Recorded output:

False
True
pencil
pencil

The copy made by with shares the array too: editing it changes what the original sees. Replacing the array with a clone separates the array storage. This is sufficient for these string elements; an array of mutable objects would still share those objects.

If element order and contents define your domain’s equality, choose an explicit sequence comparison or a value object with consistent equality and hashing. Changing the collection to an immutable collection alone does not establish element-by-element equality.

Cached properties can affect equality

A get-only auto-property has a backing field. When its value is calculated at construction, a record copy retains that stored value. An expression-bodied property computes from the current dimensions instead.

var cached = new Cached(4, 5) with { Width = 8 };
var live = new Live(4, 5) with { Width = 8 };
Console.WriteLine(cached.Area);
Console.WriteLine(cached == new Cached(8, 5));
Console.WriteLine(live.Area);
Console.WriteLine(live == new Live(8, 5));
public record Cached(int Width, int Height)
{
    public int Area { get; } = Width * Height;
}
public record Live(int Width, int Height)
{
    public int Area => Width * Height;
}

Recorded output:

20
False
40
True

The copied cached rectangle holds width 8 but area 20. A freshly constructed rectangle of width 8 has area 40, so equality fails. The live property has no stored area field. Compute simple derived values on access; carefully design cache invalidation when a cache is necessary.

Derived record types remain distinct

Message a = new Email("ready");
Message b = new Sms("ready");
Message c = new Email("ready");
Console.WriteLine(a == b);
Console.WriteLine(a == c);
Console.WriteLine(ReferenceEquals(a, c));
public abstract record Message(string Text);
public record Email(string Text) : Message(Text);
public record Sms(string Text) : Message(Text);

Recorded output:

False
True
False

The variables all have the declared type Message, but the two concrete record types differ. Two emails with matching text compare equal even though they are separate objects.

Choose equality before choosing a record

A record class is still a reference type: assignment copies its reference. Positional record classes have init-only properties; plain positional record structs have writable properties. Neither choice automatically makes referenced objects immutable.

For keys in a dictionary or members of a hash set, keep every value used by equality and hashing stable. A mutable record can move logically to a different hash bucket after insertion; successful lookup is then no longer reliable.

Use records for data whose equality you can explain. Use identity-based objects when the identity of a particular instance is what matters. For property initialization, continue with the C# learning guide.

Sources

Microsoft’s record reference covers synthesized equality, backing data, copying and inheritance. Sources checked October 15, 2026.

Share This