Record types in c# 9
In Domain driven design, we try to make a value object and Entity. All value objects should be immutable, that means, we should not try to change the state of a value object, instead we should always create a new object.
In order to make this happen, we have to write lots of code because, by default all reference types in c# are mutable. But with C#9 we got a new type called record, which by default is immutable.
Two variables of a record type are equal if the record type definitions are identical, and if for every field, the values in both records are equal. Two variables of a class type are equal if the objects referred to are the same class type and the variables refer to the same object.
*Below content is taken from MSDN
public record Person
{
public string LastName { get; }
public string FirstName { get; }
public Person(string first, string last) => (FirstName, LastName) = (first, last);
}
The record definition creates a Person type that contains two readonly properties: FirstName and LastName. The Person type is a reference type. When you define a record type, the compiler synthesizes several other methods for you:
Methods for value-based equality comparisons Override for GetHashCode() Copy and Clone members PrintMembers and ToString()
More information can be found in docs.microsoft.com/en-us/dotnet/csharp/tuto..