C# 10 Record Structs: Optimization

On this page

C# 9 Records were reference types (classes). C# 10 introduces `record struct`, bringing immutability features to value types, eliminating heap allocations for small DTOs.

Syntax

C#4 lines
public readonly record struct Point(int X, int Y);

var p1 = new Point(1, 2);
var p2 = p1 with { X = 5 }; // Non-destructive mutation

Performance Benefit

Since it’s a struct, `Point` lives on the stack. No GC pressure for creating thousands of them in a loop.

Key Takeaways

  • Use `readonly record struct` for small, immutable DTOs (< 64 bytes).
  • Use `record class` (or just `record`) for larger objects or when identity matters.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.