0Pricing
C# Academy · Lesson

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

  1. Record Types: Basics & Syntax
  2. Immutability with init & with
  3. Value Equality & Deconstruction
  4. Records in Domain-Driven Design
← Back to C# Academy