Developer knowledge network · moderated exchange

UnreliableCode-Community

Community für Entwicklerforschung, Reverse Engineering und Codierung

Knowledge indexLive
4Categories
919Threads
2.8KBeiträge
Discussion

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Vertreter: 103
Beitrittsdatum: Apr 2018
Beiträge: 40
Danke: 24
Vor 1 Monaten · 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
Vertreter: 138
Beitrittsdatum: Jul 2021
Beiträge: 10
Danke: 88
Vor 1 Monaten · 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
Vertreter: 120
Beitrittsdatum: Feb 2020
Beiträge: 12
Danke: 75
Vor 1 Monaten · Jul 19, 2026 8:22 PM
#3

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