Developer knowledge network · moderated exchange

UnreliableCode 커뮤니티

개발자 연구, 리버스 엔지니어링 및 코딩 커뮤니티

Knowledge index살다
4Categories
919Threads
2.8K게시물
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.