Record TypesAdvanced
1Question 1 of 1
csharpWhat are Record Types and how do they provide value equality in C#?
Explanation & Concept
Introduced in C# 9, `record` is a reference type (or value type via `record struct`) with built-in value-based equality semantics.
Two record instances with identical property values evaluate to `true` when compared with `==`. Records also support non-destructive mutation via the `with` expression.
Code Example
csharp
public record Point(int X, int Y);
public class Program {
public static void Main() {
var p1 = new Point(10, 20);
var p2 = new Point(10, 20);
// Value equality (evaluates to true!)
Console.WriteLine(p1 == p2); // True
// Non-destructive mutation using 'with'
var p3 = p1 with { Y = 99 };
Console.WriteLine(p3); // Point { X = 10, Y = 99 }
}
}How It Works & Key Takeaways
Records provide concise, immutable Domain-Driven Design (DDD) Value Objects and DTOs with zero manual equality boilerplate.
Mock Test · 1 Questions · Optional
Ready for Pattern Matching?
Take a quick test to evaluate yourself, or move straight to the next module.
Advanced1 questions