C# Tutorial: Records and Value Objects
record types, value equality, non-destructive mutation with with, and positional records.
Records
Records are reference types optimised for immutable data with value-based equality.
A `record` auto-generates: constructor, properties, equality, GetHashCode, and ToString.
What you'll learn in this C# records and value objects tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 15 minutes.
- Define a record — Records are concise immutable data containers.
- Value equality — Records compare by value, not by reference.
- Non-destructive mutation with with — `with` creates a copy with some properties changed.
- Record deconstruction — Positional records support deconstruction.
- Record with custom members — Add computed properties and methods to records.
- Record struct — `record struct` is a value type record (stack-allocated for small types).
- Inheritance with records — Records can inherit from other records.
- Records as DTOs — Records excel as Data Transfer Objects (DTOs) in APIs.
C# Records and Value Objects concepts covered
- Records