Developer knowledge network ยท moderated exchange

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Knowledge indexLive
4Categories
919Threads
2.8KPosts
Discussion

C# Record Types, Positional Syntax, and Value-Based Equality under the hood [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rep: 103
Join Date: Apr 2018
Posts: 40
Thanks: 24
1 months ago ยท Jul 19, 2026 5:48 AM
#1

How C# record class and record struct differ from standard classes:

CSHARP
public record User(int Id, string Username, string Email);

Compiler-Synthesized Features:

  1. Value-Based Equality: Two record instances with identical property values compare equal (user1 == user2 is true).
  2. Non-Destructive Mutation (with expression): var updated = user with { Email = "new@domain.com" };
  3. Deconstruction: var (id, name, email) = user;
  4. Formatted ToString(): Automatically prints User { Id = 1, Username = Alice, Email = ... } for clean logging.
generic_math_geek
C# 11 Math Guru
MEMBER
Rep: 138
Join Date: Jul 2021
Posts: 10
Thanks: 88
1 months ago ยท Jul 19, 2026 8:12 AM
#2

Use record struct when you want lightweight value semantics without heap allocations, and record class for immutable reference data transfer objects (DTOs).

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Rep: 120
Join Date: Feb 2020
Posts: 12
Thanks: 75
1 months ago ยท Jul 19, 2026 8:22 PM
#3

The with expression performs a memberwise clone under the hood and applies the specified property overrides cleanly.