C# 9 added the record type, which is a new reference type that uses value-based equality.
C# 10 introduced the record struct syntax to define a value type with similar properties to record (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record).
It seems unusual to create a value type version of a type that was created to be a reference type that also has value-based equality - that would surely remove most of the benefit of using the type.
Why would you ever want to declare a record struct?
Is there something more that I am missing?
The main benefits of using
record structare as follows:structdefinition to a single line==and!=operators, so these can be used for comparisons with no extra code to define the operator overloads. Withstruct, you can only do comparisons using theEquals()method by default.ToString()method thanstruct. Therecord structToString()method will produce therecord structname, the names of its properties and their values. ThestructdefaultToString()method only produces thestructname.struct(up to 20 times faster - https://anthonygiretti.com/2021/08/03/introducing-c-10-record-struct/)In some ways,
recordis similar to a value tuples which provide default operator overloads and have aToString()method that is closer torecord struct(value tuples'ToString()method produces the values of all of their properties).However, value tuples are only used on the fly, whereas
record structcan be used to define type that will be repeatedly used.Note
record/record classis immutable by default butrecord structis not, so if you want an immutablerecord struct, you must usereadonly record struct.Final Remark
Considering the benefits of using
record structoverstruct, it's probably best to always preferrecord structunless there is some very specific reason not to.It seems that
record structis an enhancement ofstruct, leaving the old type so that existing behaviour/functionality ofstructis not removed.