The feature of records in C# is underrated, but it offers substantial benefits. Records are ideal for checking value equality between variables of the same type.

While classes require additional code to achieve value-based equality, records simplify this by automatically handling various tasks:

  • Implementing IEquatable<T> and overriding GetHashCode()
  • Overriding ToString() to aid in debugging by displaying property values
  • Deconstructing a record into a tuple, facilitating tasks like pattern matching

These features are encompassed within the record keyword.

For applications adhering to Domain Driven Design (DDD), where types are categorized into Aggregates, Entities, and Value Objects, records are a natural fit for Value Objects. They also work well as models or data transfer objects (DTOs).

A key advantage of records is their immutability. Once created, a record's state cannot be altered. This characteristic aligns well with functional programming practices.

Use records in these scenarios to reduce the boilerplate code otherwise required when using classes.