Skip to content

record struct

Context: record struct (C# 10+) is a value type record that provides value semantics for structs.

public record struct Point(int X, int Y);
  • Value type (allocated on stack)
  • Mutable by default (can add readonly modifier)
  • Provides same equality and ToString benefits as record class
public readonly record struct ImmutablePoint(int X, int Y);
var p = new ImmutablePoint(3, 4);
// p.X = 5; // error: readonly

Geometric calculations: Use record struct for small, immutable structures like Point, Size, Rectangle to avoid heap allocations and get value equality for free.

Example: In .NET, System.Drawing.Point is a struct but not a record. Upgrading to record struct would add deconstruction and equality.