Understanding C# Record Types: A Comprehensive Guide
Introduced in C# 9.0, record types are a new reference type that provide a concise syntax for defining immutable reference types. They are designed to simplify the creation of value types and make it easier to work with data. Let's delve into the world of C# record types, exploring their benefits, syntax, and how they compare to traditional classes.
Why Use Record Types?
Record types offer several advantages over traditional classes, especially when dealing with data. Here are some key benefits:
- Immutability: Once created, the values of a record cannot be changed. This ensures data consistency and makes records easier to reason about.
- Equality and Hash Code: Records automatically implement
EqualsandGetHashCodemethods, making them ideal for use in collections and as dictionary keys. - ToString: Records also provide a useful
ToStringimplementation, which can be handy for debugging and logging. - Concise Syntax: Records allow for a more streamlined way to define value types, reducing boilerplate code.
Defining a Record Type
Defining a record type is straightforward. Here's a simple example of a record representing a person:

public record Person(string FirstName, string LastName);
In this example, Person is a record with two properties: FirstName and LastName. Records can also have initializers and other features found in regular classes.
Record Structs and With Expressions
Records can also be defined as structs, providing value semantics. This can be useful for small, immutable data structures. Additionally, records support the with expression, allowing you to create a new record with modified properties while keeping the original values unchanged:
var person = new Person("John", "Doe");
var updatedPerson = person with { LastName = "Smith" };
Record Types vs. Traditional Classes
While record types share some similarities with traditional classes, they are designed for different use cases. Records are ideal for data-focused scenarios, where immutability and automatic implementation of equality and hash code methods are beneficial. Classes, on the other hand, are more suitable for stateful objects with behavior.

Records and Inheritance
Records support inheritance, allowing you to create a hierarchy of records. However, records are sealed by default, preventing them from being used as base classes. To make a record non-sealed, you can use the unsealed keyword:
public unsealed record Person(string FirstName, string LastName);
Records and Performance
Records are designed to be efficient, with a performance profile similar to structs. This makes them an attractive choice for high-performance scenarios. However, it's essential to consider the trade-offs and use records judiciously, as overusing them can lead to increased garbage collection pressure.
Conclusion
C# record types are a powerful addition to the language, offering a concise and expressive way to define immutable reference types. By understanding and leveraging record types, developers can create more maintainable, performant, and data-focused code. As with any new feature, it's essential to evaluate whether record types are the right tool for the job, but they are certainly worth considering in many scenarios.





















