C# 10 Record Structs

Joe Mayo
General Thoughts
Published in
5 min readNov 23, 2021

--

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 of record class to represent what we know of as a C# 9 record. 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…

--

--

Joe Mayo
General Thoughts

Author, Instructor, & Independent Consultant. Author of C# Cookbook: — http://bit.ly/CSharpCookbook — @OReillyMedia #ai #csharp #Web3