When to Use C# Record Types Instead of Classes or Structs in .NET Applications
Explore how C# record types give value‑based equality and `with` expressions, when they fit, and their limits compared to classes and structs.
23 Dec 2025, 10:14 UTC

Problem: mutable classes cause unexpected equality bugs
In many .NET projects developers create classes to hold data, then rely on the default Equals implementation. Because classes compare by reference, two objects that look identical can be considered different, leading to subtle bugs in collections, caching, and state management.
Thesis: records give concise, value‑based types when data matters
C# record types, introduced in C# 9.0, provide immutable, data‑centric objects that automatically implement value‑based equality, a helpful with expression for nondestructive mutation, and a clear distinction between reference‑type record class and value‑type record struct.
What records are
A record is a reference‑type (unless declared record struct) that the compiler treats as a value‑based type. For a positional record like public record Person(string FirstName, string LastName), the compiler generates:
- an
Equalsmethod that compares all properties, - a
GetHashCodeimplementation, - an
IEquatable<T>interface, - a useful
ToStringoverride, and - a
Deconstructmethod.
These members make records ideal for domain models, DTOs, or any scenario where the logical identity of an object is defined by its data, not its memory address.
`with` expression for nondestructive mutation
Instead of writing a manual cloning method, you can create a copy with selected changes:
var original = new Person("Alice", "Smith");
var updated = original with { FirstName = "Alicia" }; // FirstName changed, LastName unchanged
The with expression builds a new instance while preserving the rest of the properties, eliminating boilerplate and reducing mutation errors.
Choosing between record, class, struct
Records are not a universal replacement. Use a record class when you need inheritance, mutable reference semantics, or complex behavior that classes naturally support. Choose a record struct for value‑type semantics, such as lightweight collections that benefit from stack allocation. Avoid records when you require deep immutability (reference‑type properties can still be mutated) or when the type’s identity must be based on reference equality rather than value equality.
Worked example
Create a console project targeting .NET 6 or later and set <LangVersion>9.0</LangVersion> in the project file. Then add:
public record Person(string FirstName, string LastName);
Compile with dotnet build. Inspect the generated IL or decompile the assembly to see the synthesized Equals and GetHashCode methods. Run a simple test:
var a = new Person("Bob", "Jones");
var b = new Person("Bob", "Jones");
Console.WriteLine(a == b); // True, value‑based equality
Console.WriteLine(ReferenceEquals(a, b)); // False, different instances
Now demonstrate with:
var c = a with { FirstName = "Robert" };
Console.WriteLine(c.FirstName); // Robert
Console.WriteLine(c == a); // False, different values
Limitation and practical check
Records are shallowly immutable: if a property is a reference type, its contents can still be mutated after construction, breaking the intended immutability. Also, value‑based equality can be slower than reference equality for large objects because the compiler must compare each member. Inheritance is limited to the same record kind; a positional record cannot be directly inherited, and derived types must explicitly call the base constructor.
To verify the behavior, compare two class instances with the same data using == (reference equality) and two record instances with identical data (value equality). Use dotnet --version to ensure the compiler supports C# 9.0 or later, and run csc -langversion if you need to confirm the language version.
Closing: actionable steps
1. Start a new console app targeting .NET 6+ and enable C# 9.0.
2. Define a positional record for the data shape you need.
3. Use the with expression to create updated copies instead of manual cloning.
4. Verify value‑based equality by comparing == or Equals on instances with identical data.
5. If you need mutable state or complex inheritance, stick with a regular class; if you need stack‑allocated, lightweight data, consider a record struct.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.