Skip to content
AgileLogics
4 min read

Records

Prerequisites: Nullable types & null safety

A record (C# 9+) is a compact, immutable-by-default data carrier — directly parallel to Java's record.

public record Point(int X, int Y);

var p = new Point(3, 4);
Console.WriteLine(p.X);              // 3
Console.WriteLine(p);                 // Point { X = 3, Y = 4 }
Console.WriteLine(p == new Point(3, 4)); // true — value equality

A with expression creates a modified copy rather than mutating the original — the idiomatic way to "change" an immutable record:

var moved = p with { Y = 10 }; // Point { X = 3, Y = 10 } — a new object