Record Types: Basics & Syntax
Declare record classes and record structs, understand positional syntax, and compare records vs classes.
What Are Record Types?
Records (introduced in C# 9) are reference types designed for immutable data with value semantics. They automatically generate equality based on property values, a readable ToString(), and support non-destructive mutation via with expressions.
Positional Record Syntax
The positional syntax is the most concise form. Properties, constructor, deconstruction, and equality are all generated from a single line.
// All in one line:
public record Point(double X, double Y);
public record Person(string FirstName, string LastName, int Age);
// Generated automatically:
// - Primary constructor: new Point(1.0, 2.0)
// - Deconstruct: var (x, y) = point;
// - Equality: p1 == p2 compares X and Y values
// - ToString: "Point { X = 1, Y = 2 }"All lessons in this course
- Record Types: Basics & Syntax
- Immutability with init & with
- Value Equality & Deconstruction
- Records in Domain-Driven Design