Value Equality & Deconstruction
Understand record structural equality, override Equals/GetHashCode, and use deconstruction patterns.
Record Equality Deep Dive
Records automatically generate Equals, GetHashCode, and operators ==/!= that compare all properties structurally. Understanding how this works helps you use records correctly and override when needed.
How Auto-Generated Equality Works
The compiler generates code that compares each property using EqualityComparer<T>.Default. For reference-type properties, this is their own Equals — not reference equality.
public record Address(string Street, string City);
public record Person(string Name, Address HomeAddress);
var a = new Person("Alice", new Address("123 Main", "NYC"));
var b = new Person("Alice", new Address("123 Main", "NYC"));
Console.WriteLine(a == b); // True — deep structural equality
// Equality checks:
// Name: "Alice" == "Alice" ✓
// HomeAddress: compares Address properties recursively ✓All lessons in this course
- Record Types: Basics & Syntax
- Immutability with init & with
- Value Equality & Deconstruction
- Records in Domain-Driven Design