C# 10 Record Structs: Value Type Optimization

In C# 9, we got `record` (which was a reference type). In C# 10, we get `record struct`.

Syntax

public record struct Point(int X, int Y);

Performance Characteristics

Because it’s a struct, `Point` is allocated on the stack. No GC pressure! Because it’s a record, you get:

  • Value-based equality (automatically implemented)
  • Deconstruction
  • `ToString()` formatting

Be careful: structs are copied by value. If your record struct is large (more than 16 bytes), passing it around might be slower than passing a reference. For simple data like Coordinates, Vector3, or ComplexNumber, `record struct` is the perfect tool.


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

Subscribe to get the latest posts sent to your email.

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.