C# Records
Today I will focus on records, a feature introduced in C#9, but I will also talk about record structs, which were added in C#10.
If you are using .NET for development, you might want to check this matrix first to learn if you can use this feature, since it is still pretty new.
STS - Standard term support (usually one year)
LTS - Long-term support (usually three years)
Records, the new reference type
Record is a new reference type that you can create instead of classes or structs, but you will always see it next to the class or struct keyword. This means that it will always be declared as “record class” or “record struct”. Sometimes you might see only “record" declared, but that is usually just a sugarcoating syntax for “record class”.
Mutable and Immutable
Before going to the next phase, where we will discuss what records bring to the table, I would like to talk briefly about mutable and immutable. You should understand why this is important before going further.
When we declare a record class using positional syntax, by default, it will be immutable, but if we create a record class using nominal creation (the usual way of creating a class, with a constructor and get+set), it can be both immutable and mutable. I will explain why further down the line.
Something similar happens with struct. By default, record struct is mutable.
The following statement is very important to know when you work with records, especially record struct. A record struct will be immutable only if we declare it like readonly record struct.
1. Mutable record class using nominal creation
2. Immutable record class using nominal creation
As we can see, this record class is immutable, because it contains init instead of set.
3. Mutable record struct
When people say that record struct is not immutable by default, they are referring to the positional syntax, which by default is not immutable.
4. Immutable record struct
Here we can see that we can’t initialize the property anymore, thus making it immutable.
New additions and benefits of records
1. Positional syntax
2. New ToString output
3. Value-based equality
The code for CoordsRecordClass and CoordsClass can be found at point 1. Primary construct syntax
4. Non-destructive mutation
The goal of this non-destructive mutation is to help you easily create immutable records.
When to use
Records should be created as an immutable reference type, so, hmm, let’s see, when do we want to use immutable data? Should it be when we send data from A to B, especially through the network?
What kind of classes are the ones created for the purpose of being sent through the internet, communicating with the client application, and from the client application to the rest of the application?
If the above questions still didn’t ring a bell, we usually want to use records with DTOs.
Wrap-up
I hope this helped you understand records better. Until next time, based on DTOs, I recommend you look into Anemic models.
The code in this article is simplified for demonstration purposes and it was written in .NET7 with C#11.
The entire code used for this article can be found here - Github profile.