Search⌘ K
AI Features

Working with Records

Explore how to work with records and init-only properties in C# 9 to create immutable objects. Understand the concept of non-destructive mutation using records and the with keyword, which enables creating modified copies without changing the original. This lesson helps you implement safer and more maintainable object-oriented code using modern C# features.

Before we dive into the new records language feature, let us see some other related new features of C# 9 and later.

Init-only properties

We have used object initialization syntax to instantiate objects and set initial properties. Those properties can also be changed after instantiation. Sometimes, we want to treat properties like read-only fields so they can be set during instantiation but not after. The new init ...