Developer knowledge network ยท moderated exchange

Komunitas Kode Tidak Dapat Diandalkan

Riset Pengembang, Rekayasa Terbalik & Komunitas Pengkodean

Knowledge indexHidup
4Categories
919Threads
2.8KPostingan
Discussion

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Reputasi: 103
Tanggal Bergabung: Apr 2018
Postingan: 40
Terima kasih: 24
1 bulan yang lalu ยท 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
Reputasi: 138
Tanggal Bergabung: Jul 2021
Postingan: 10
Terima kasih: 88
1 bulan yang lalu ยท 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
Reputasi: 120
Tanggal Bergabung: Feb 2020
Postingan: 12
Terima kasih: 75
1 bulan yang lalu ยท Jul 19, 2026 8:22 PM
#3

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