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);Characteristics
Section titled “Characteristics”- Value type (allocated on stack)
- Mutable by default (can add
readonlymodifier) - Provides same equality and
ToStringbenefits asrecord class
public readonly record struct ImmutablePoint(int X, int Y);var p = new ImmutablePoint(3, 4);// p.X = 5; // error: readonlyReal-world usage example
Section titled “Real-world usage example”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.