The record structs
The record types introduced in C# 9 provide type declaration to create immutable reference types with synthesized methods for equality check and ToString. C# 10 brings us the record structs. In this section, we will see what record struct is and how it is different from record class.
We use the record keyword to declare record class, and we use the same record keyword to declare record struct, as shown in the following code:
public record struct Employee(string Name);
Note
In C# 9, to declare a record class, we don't explicitly use the class keyword. We simply specify record to declare it as shown here: public record Shape(string Name);
Simply using the record keyword will continue to work in C# 10 to declare a record class, but it is recommended to specify class or struct explicitly for better readability.
record struct offers similar benefits to what record class offers, such as the following:
- Simplified declaration syntax
- Value...