You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
C# evolves rapidly with annual releases. This lesson covers the most impactful features from recent versions (C# 9 through C# 12) that every modern C# developer should know.
Records are immutable reference types with built-in value equality:
// Record declaration
public record Person(string Name, int Age);
// Usage
var alice = new Person("Alice", 30);
var alice2 = new Person("Alice", 30);
Console.WriteLine(alice == alice2); // True (value equality!)
Console.WriteLine(alice); // Person { Name = Alice, Age = 30 }
// Non-destructive mutation with 'with'
var older = alice with { Age = 31 };
Console.WriteLine(older); // Person { Name = Alice, Age = 31 }
| Type | Syntax | Value/Reference | Mutable |
|---|---|---|---|
record (or record class) | record Person(string Name); | Reference | Immutable by default |
record struct | record struct Point(int X, int Y); | Value | Mutable by default |
readonly record struct | readonly record struct Point(int X, int Y); | Value | Immutable |
Tip: Use records for data transfer objects (DTOs), domain models, and any type where value equality makes sense.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.