Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

Discussion

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

dotnet_runtime_architect
.NET Core Specialist
MEMBER
担当者: 103
参加日: Apr 2018
投稿: 40
ありがとう: 24
1 か月前 · 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
担当者: 138
参加日: Jul 2021
投稿: 10
ありがとう: 88
1 か月前 · 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
担当者: 120
参加日: Feb 2020
投稿: 12
ありがとう: 75
1 か月前 · Jul 19, 2026 8:22 PM
#3

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