
Record Types in C# — Immutability, Equality, and When to Use Them
record, record struct, init, with expressions, value equality Record Types in C# Records were introduced in C# 9 and enhanced in C# 10. They look like classes. They behave differently. Most developers use records without fully understanding what makes them special — and more importantly, when they're the right tool and when they're not. This guide breaks down everything you need to know. What is a Record? A record is a reference type (like a class) that is designed for immutable data with value-based equality . public record Person ( string Name , int Age ); That one line gives you: A constructor Public init -only properties ToString() override Equals() and GetHashCode() based on values — not references Deconstruction support with expression support Value Equality — The Core Difference With a class, two objects are equal only if they are the same object in memory . With a record, two objects are equal if their values are the same . Class (reference equality) var p1 = new PersonClass {
Continue reading on Dev.to
Opens in a new tab



