C# and .NET • Modern C# baseline
Records and Value-Based Data
Use records for data whose identity is defined by its contents.
A record class provides value-based equality and concise data-oriented syntax. A record struct is a value type with different copying and allocation behavior. Records are not automatically deeply immutable: referenced objects may still be mutable, so choose member types and init behavior carefully.
Key points
- Use records for messages, results, and immutable-style data transfer.
- Prefer classes for objects with identity and managed lifecycle.
Example
var first = new TaskItem("Write tests", false);
var updated = first with { IsComplete = true };
Console.WriteLine(updated);
public record TaskItem(string Title, bool IsComplete);
What to expect
TaskItem { Title = Write tests, IsComplete = True }
Practice exercise
Compare two records with the same values and then change one field. Explain the equality result.
Key idea: Choose record semantics when value equality matches the domain.