C# 10 Record Structs
C# 9 introduced a new type, record
, that was an immutable reference type with value equality. The new follow-up to this feature in C# 10 was the record struct
.
Without getting ahead of the content in this post too much, one of the new features of
record
in C# 10 is allowing the alias ofrecord class
to represent what we know of as a C# 9record
. Moving forward, I'll use this new naming to clarify which type I'm talking about.
This post discusses the new record struct
: explaining potential benefits and discussing the similarities as well as differences with record class
.
Features the record class
type
Before going into the similarities and differences of record class
and record struct
, lets discuss a few features of record class
types:
- Simple declaration syntax
- Reference semantics
- Value equality
- Readable ToString output
- Built-in deconstruction
- Inheritance hierarchies
- Immutability
What’s nice about this feature set is that you still have a lot of flexibility in your implementation. Here are a few examples:
- You can opt out of the simple syntax and write the record like a normal class.
- You can choose which properties are immutable and which aren’t.
- You can implement
IEquatable
and override the default…