
Editorial illustration.
A coordinate, measurement or compact identifier often represents a value: two instances with the same fields should count as equal. C#’s record struct can express that directly, but it also has value-type behavior. Assignment copies the value, and large or mutable structs can surprise callers. Use one when the data is small, self-contained and naturally compared by its fields—not simply because the keyword looks concise.
Summary
record structis a C# value type with compiler-generated value equality and useful record syntax.- It fits small, self-contained values such as coordinates or measurements.
- Assignment copies the struct; nested reference objects such as arrays are still shared references.
- Prefer a record class when identity, inheritance or reference semantics matter.
The language feature and prerequisites
record struct was introduced in C# 10. A project needs a compiler that supports C# 10; .NET 6 projects commonly use it by default, but the language version is determined by the compiler and project configuration. The feature belongs to the C# language, while the project’s target framework and SDK determine which APIs and toolchain are available. Microsoft documents the syntax and behavior in its C# record reference.
Here is a small value type for a map coordinate:
public readonly record struct MapPoint(double Latitude, double Longitude);
var home = new MapPoint(3.1390, 101.6869);
var copy = home;
Console.WriteLine(home == copy); // True: same values
A record struct is a struct, so assigning home to copy copies its fields. The generated equality checks values for the declared members. That is different from an ordinary class, where == usually checks whether two variables refer to the same object unless the class defines different behavior.
Choose it when the data is genuinely a value
A useful test is to ask whether identity matters. If two coordinates with the same latitude and longitude are interchangeable in your code, value equality is natural. The same can be true for a money amount with currency, a pixel size or a range boundary—provided the type remains small and its equality rules match the domain.
readonly record struct is a good default for an immutable value. It prevents members from being reassigned after construction and communicates that callers should treat the value as fixed. A plain record struct has writable properties generated for positional parameters, allowing state changes. Use that flexibility only when a mutable value is part of the design; mutation is easy to overlook because assigning a struct creates another copy.
For example:
public readonly record struct Celsius(decimal Value)
{
public decimal Fahrenheit => Value * 9m / 5m + 32m;
}
var morning = new Celsius(18m);
var afternoon = morning with { Value = 24m };
Console.WriteLine(morning.Value); // 18
Console.WriteLine(afternoon.Value); // 24
The with expression creates a copy with the selected property changed. Here it is readable because the value is small and immutable. A value is still copied; the expression does not mutate morning.
Watch the shallow-copy boundary
Value semantics apply to the struct’s fields, but a reference-type field still contains a reference. Copying the struct copies that reference, not the object it points to. For instance, if a record struct contains a string[], both copies refer to the same array. Generated equality also compares members according to their equality behavior; arrays use reference equality rather than element-by-element sequence equality.
That means this can be misleading:
public record struct SearchResult(string Query, string[] Matches);
var first = new SearchResult("cat", new[] { "cat", "catalog" });
var second = first;
second.Matches[0] = "changed";
Console.WriteLine(first.Matches[0]); // changed: the array is shared
This example stays within C# 10 syntax. Better still, avoid mutable reference members in a value object, or use an immutable collection and define equality deliberately if sequence contents should determine equality.
When a record class is a better fit
Use a record class when consumers should share a reference, when the object is large, or when you need inheritance. A record class still provides value-based equality by default, but assigning it copies the reference. A record struct cannot inherit from another class and carries the costs and constraints of a value type. The C# guide’s record class and record struct comparison explains those differences.
An entity such as a customer, order or database row usually has identity: two separate rows with matching fields are not automatically the same entity. In that case, use a class and define equality based on the entity’s identifier if the application needs it. Microsoft also notes that records are not appropriate as Entity Framework Core entity types because EF relies on reference equality to ensure one instance per conceptual entity.
Do not choose a struct only to chase performance. Large structs can cost more to copy, and measuring a tiny benchmark may not reflect the real application. Prefer a clear model first. If a value type is on a hot path, benchmark the actual workload and look at allocation and copying behavior together.
A decision checklist
- Does identity matter? If yes, prefer a class-based entity.
- Are equal member values enough to define equality? If no, define the equality rule explicitly or choose another type.
- Is the value small and self-contained? If it contains large mutable objects or collections, reconsider.
- Should callers mutate it? If not, use
readonly record struct. - Does copying make sense? A value should remain understandable when passed or assigned by value.
- Does the project compiler support C# 10? Check the project’s SDK and language version rather than assuming from the installed runtime alone.
This guide to safe dictionary lookups with TryGetValue shows another place where type equality matters: dictionary keys depend on stable equality and hash codes. For data-processing examples, see batching a sequence with Enumerable.Chunk, which targets .NET 6 and later.
Common mistakes
- Treating a record struct like a reference object and expecting mutation to affect every copy.
- Putting mutable arrays or lists inside it and assuming a deep copy occurs.
- Using it for a database entity where reference identity is important.
- Assuming record equality compares every nested collection element.
- Using a large struct to avoid allocations without measuring the actual program.
- Forgetting that C# language version and target framework are related but separate project settings.
My Take
I reach for readonly record struct when I can describe the type as a small value in a sentence: “a point is two coordinates,” or “a temperature is one measurement.” If the sentence starts describing identity, lifetime or shared mutable state, a class is usually easier to reason about.
Conclusion
Use record struct when value equality and copy-on-assignment match the domain and the data is small. Prefer the readonly form for immutable values. Choose a class when identity, inheritance, shared state or large object graphs matter. These semantics are the feature; the concise syntax is only a bonus.