C# / .NET Compendium PLEN
.NET / C# / C# — language fundamentals

The difference between struct and record

This question mixes two axes

struct and record answer two different questions: - struct vs class - a question about memory semantics (value type vs reference type) - record vs an ordinary declaration - a question about equality semantics

record is NOT an alternative to struct - it is a modifier you can use together with class (the default) or together with struct (explicitly, via record struct).

Four combinations, not two

public class OrderClass { }               // reference type, equality by reference
public record OrderRecord { }             // reference type (!), equality by value
public struct PointStruct { }             // value type, equality by value (but manual/reflection)
public record struct PointRecordStruct { } // value type, equality by value (generated)

A record without the struct suffix generates a class by default.

public record OrderRecord(int Id); // to jest DE FACTO klasa!

OrderRecord r1 = new OrderRecord(1);
OrderRecord r2 = r1;
// r1 and r2 are STILL two references to THE SAME object on the heap (as with a class)
// BUT r1 == r2 returns true regardless, because it is a VALUE comparison, not a reference one

Comparing a "pure" struct with a "pure" record

struct record (= record class)
Memory type Value (stack/inline) Reference (heap)
Copying on assignment Copies the WHOLE object Copies only the reference
Default equality Field comparison via reflection (slow!) in ValueType.Equals Generated, efficient code comparing the fields
Can be null No Yes
Inheritance No Yes
with expression No Yes

A surprising fact: a plain struct also has "equality by value" in a sense:

public struct PointStruct { public int X; public int Y; }

var p1 = new PointStruct { X = 1, Y = 2 };
var p2 = new PointStruct { X = 1, Y = 2 };
p1.Equals(p2); // true - BUT via reflection, SLOW (ValueType.Equals uses reflection)

record struct solves exactly this performance problem.

record struct - where the two concepts meet

public record struct PointRecordStruct(int X, int Y);
var p1 = new PointRecordStruct(1, 2);
var p2 = p1; // KOPIUJE (struct)
p2.X = 100; // CAN be changed - a record struct WITHOUT readonly is not init-only

Console.WriteLine(p1.X); // 1 - niezmienione
var p3 = p1 with { X = 100 }; // "with" works for a record struct too

An important difference from record class: a record struct is NOT init-only by default - the properties have { get; set; }, not { get; init; } (unless you add readonly record struct). This is the opposite of what happens in record class.

public record struct MutablePoint(int X, int Y); // set, CAN be changed after creation
public readonly record struct ImmutablePoint(int X, int Y); // init, niemutowalne, jak record class

A practical rule

Do you need to represent DATA (not behaviour/identity)?
├── Small (≤16 bytes), a value like a primitive (Point, Money, coordinates)?
│   └── record struct (the best of both worlds)
├── Larger data, DTO, Command/Query (CQRS)?
│   └── record (= record class)
└── Need mutable state with identity (an EF entity, a ViewModel, a service)?
    └── a plain class
Buy me a coffee