Said's Blog

C# - Record structs

September 24, 20223 min read
image

After introducing records in C# 9, Microsoft brought in C# 10 record structs which carry a lot of advantages such as immutability and equality comparison.

Syntax

Microsoft has made an improvement to record classes. With C# 9, to declare a record you replaced the “class” keyword with “record.” To avoid confusion when declaring structs as records, C# 10 allows a new syntax to declare a class as a record by mixing record and class

Syntax

Immutability

With Immutability

Positional records

Positional records

With expression

With expression

Value base comparison

Value base comparison

Printing members

Printing members

Performance

In terms of performance, using record structs is 20 times faster than a regular struct according to benchmarks. For more details, check this article.