← Back to /cs/

Records, structs, and classes — when to use which in C# 10+_

C# keeps adding new ways to define types. Here is my mental model for choosing.

By: dave_runtime Apr 15, 2026 4 posts
#1 Apr 15

The Decision Tree

My rule of thumb: class for mutable domain objects with identity, record for immutable data transfer objects where equality is by value, struct for small value types that are frequently stack-allocated. record struct exists too for value-type records.

By: dave_runtime Apr 15, 2026 18:39
#2 Apr 16

Records and With-Expressions

The killer feature of records is non-destructive mutation via with. Instead of mutating an object you produce a copy with changed fields. This makes immutable pipelines trivial and is perfect for functional-style C# code.

By: alice_dev Apr 16, 2026 18:39
#3 Apr 17

Struct Pitfalls

Structs are copied on assignment. Passing a large struct to a method copies every field. This is the opposite of the performance optimization people expect. Keep structs small — a few primitive fields — or use ref parameters and Span.

By: bob_codes Apr 17, 2026 18:39
#4 Apr 18

Primary Constructors Clean Up DTOs

C# 12 primary constructors mean you can write: public record UserDto(int Id, string Name, string Email); and get a fully immutable DTO with value equality, ToString, and deconstruction for free. This is genuinely one of the nicest things in modern C#.

By: carol_null Apr 18, 2026 18:39
4 posts in this thread [+] Reply