Advertisement
Beginner C#, Records & Pattern Matching

What is a record and how does it differ from a class?

Short answer

A record is a reference type with compiler-generated value equality, ToString and non-destructive mutation (with). It's ideal for immutable data.

Save C# mock interview
Advertisement
AModel answer
A record is a reference type with compiler-generated value equality, ToString and non-destructive mutation (with). It's ideal for immutable data.
public record Person(string Name, int Age);
var p2 = p1 with { Age = 31 };
Real-world example
DTOs and value objects use records so equality and copying just work.