DEV Community


Posted on • Updated on

record in C#

record keyword was introduced in c# 9.

public record Person(string FirstName, string LastName);
Enter fullscreen mode Exit fullscreen mode

In C#, the record datatype is used when we want to define an immutable type that represents a data structure. Records are introduced in C# 9.0 and are primarily used to simplify the creation of immutable types by reducing the amount of boilerplate code required.

Here are some scenarios where we might consider using the record datatype:

Data Transfer Objects (DTOs): When we need to transfer data between different layers of our application or across network boundaries, using records can provide a concise and efficient way to represent the data.

Value Objects: If we have a concept in our domain model that is primarily identified by its values rather than an identity, records can be a good choice. Value objects are immutable and should be treated as such to maintain their integrity.

Modeling Immutable Data: If we have data that doesn't change once it's created, using records can help ensure its immutability. Records have built-in value equality semantics, making them suitable for scenarios where we need to compare data objects for equality based on their properties.

Pattern Matching: Records in C# also work well with pattern matching. We can use pattern matching to deconstruct records and extract their properties, making it easier to work with data structures.

Simplifying Equality and Hash Code Implementations: Records automatically generate equality and hash code implementations based on their properties. This can save us from writing repetitive code when dealing with equality comparisons.

It's important to note that records are intended for representing data and are not suitable for all scenarios. If we need a mutable type or if we have a concept that requires identity-based comparisons, classes may be a better choice.

Overall, records provide a concise and convenient way to define immutable data structures in C#, reducing boilerplate code and improving code readability.

What will happen if we use class instead of record?

If we use a class instead of a record in C#, there are a few key differences and implications:

Mutability: Unlike records, classes are mutable by default. This means that we can change the values of the class's properties after it has been instantiated. If immutability is important for our use case, we would need to implement our own mechanisms to enforce immutability when using classes.

Equality and Hash Code: Classes do not provide built-in value equality semantics like records do. By default, two instances of a class are considered equal only if they reference the same object in memory. If we want to define custom equality semantics for a class, we need to override the Equals() and GetHashCode() methods.

Boilerplate Code: Using a class requires more boilerplate code compared to a record. With classes, we need to manually define properties, constructors, equality methods, and hash code methods. Records, on the other hand, automatically generate these implementations for us, reducing the amount of code we need to write and maintain.

Readability and Intent: Records have a more explicit and concise syntax for representing data structures. By using records, we convey the intent that the type is primarily meant for holding data, emphasizing immutability and value-based equality. This can improve code readability and make our intentions clearer to other developers.

It's worth mentioning that both classes and records have their own use cases. Classes are more versatile and suitable for scenarios where mutability, inheritance, or identity-based comparisons are required. On the other hand, records are specifically designed for representing data structures and immutability, offering a more streamlined and focused approach.

Top comments (0)