Working with record types
Sometimes, you want to treat properties like
readonly fields so that they can be set during instantiation but not after. In other words, they are immutable. The
init keyword enables this. It can be used in place of the
set keyword in a property definition.
Since this is a language feature not supported by .NET Standard 2.0, we cannot use it in the
PacktLibraryNetStandard2 project. We must use it in the modern project:
- In the
PacktLibraryModernproject, add a new file named
Records.cs, define a person class with two immutable properties, as shown in the following code:
namespace Packt.Shared; public...