The main benefits of using record struct
are as follows:
- It allows you to simplify a
struct
definition to a single line
- It provides overloads for the
==
and !=
operators, so these can be used for comparisons with no extra code to define the operator overloads. With struct
, you can only do comparisons using the Equals()
method by default.
- It provides a more comprehensive default
ToString()
method than struct
. The record struct
ToString()
method will produce the record struct
name, the names of its properties and their values. The struct
default ToString()
method only produces the struct
name.
- It provides performance benefits over
struct
In some ways, record
is similar to a value tuples which provide default operator overloads and have a ToString()
method that is closer to record struct
(value tuples' ToString()
method produces the values of all of their properties).
However, value tuples are only used on the fly, whereas record struct
can be used to define type that will be repeatedly used.
Note
record
/ record class
is immutable by default but record struct
is not, so if you want an immutable record struct
, you must use readonly record struct
.
Final Remark
Considering the benefits of using record struct
over struct
, it's probably best to always prefer record struct
unless there is some very specific reason not to.
It seems that record struct
is an enhancement of struct
, leaving the old type so that existing behaviour/functionality of struct
is not removed.
record struct
not a plainrecord
orstruct
– Windproofrecord struct
. Using a new type just to reduce code isn't really sensible- improved syntax would make more sense. Also since tuples exist, shortening code is not really a good reason to userecord struct
. Will study the rest of the articles to see what else there is. – Windproofrecord
does. It does not introduce new types, it introduces a new way to writeclass
andstruct
declarations with autogenerated boilerplate. Mere syntax cannot be retrofitted to make existing classes or structs behave this way, that would be far too complicated -- and changing how tuples work, while possible in principle, is a passed station since large amounts of existing code count on their existing behavior. – Melodeerecord
has different properties toclass
andstruct
. It is not just a way of declaring existing types with autogenerated code. Also there are many cases where new syntax and functionality has been added with existing types (usually using keyword to modify behaviour) so I'm not sure what you mean. – Windproofrecord class
is aclass
. Everyrecord struct
is astruct
. (A plainrecord
is equivalent torecord class
.) All .NET languages are capable of consuming records, even if they have no explicit support for them, precisely by virtue of the fact that records do not extend the .NET type system. It is possible to write types that behave identically to any givenrecord
declaration, with the only difference being that your members would not have a[CompilerGenerated]
attribute. – Melodeewith
expression only works with records specifically because it looks for a compiler-generatedClone
method. Since you can't declare such a method yourself, this is the one aspect where records are distinguishable from non-records on the language level. (Although this argument specifically only applies torecord class
es and does not apply torecord struct
s, sincewith
works on all structs.) – Melodeerecord
is a new reference type,record class
is the same plainrecord
- the syntax just makes it clear that it's reference type.record struct
is arecord
that is value type: learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/… – Windproofrecord struct
s as well. And indeed,record
is the same thing asrecord class
now. Nothing of that invalidates what I said. The docs are misleading to the point of being incorrect when they talk ofrecord
s as "a new reference type that you can create instead of classes or structs"; records wouldn't be half as useful if they weren't also plain old classes to existing code! Value-based equality and immutability can be implemented on classes manually, it's just tedious.record
eliminates this tedium. – Melodee