C# Interview Question #78
What is a record in C#?
Memory Management & Modern C# Mid-Level Intermediate
Quick Interview Answer
A record is a type designed primarily for data-centric models and provides value-based equality by default.
Detailed Explanation
A record is a type designed primarily for data-centric models and provides value-based equality by default.
Records are useful for DTOs, messages, commands, responses, and immutable-style models. Record classes are reference types, while record structs are value types.
Records also support concise positional syntax and non-destructive mutation through the with expression.
Code Example
public record ProductDto(
int Id,
string Name,
decimal Price);
var p1 = new ProductDto(1, "Laptop", 1000);
var p2 = p1 with { Price = 900 };