Record in C#

Introduction

Record types are a powerful feature introduced in C# 9.0 that simplifies the creation of immutable objects while providing a concise and expressive syntax. They are designed to reduce boilerplate code and make your codebase more maintainable. In this blog post, we will explore what record types are, how to create and use them in C#, and why they are a valuable addition to the language.

What are Record Types?

A record type is a value type in C# that combines the features of a class and a struct with an emphasis on immutability and value-based equality. They are particularly useful for representing data structures that do not change once they are created. Record types are defined using the record keyword.

Creating a Record Type?

Creating a record type is straightforward. You define it like a class or struct but with the record keyword:

The tools which I have leveraged for this tutorial are given below:

  1. Visual Studio Community Edition
  2. .NET 6.0
  3. Console App

The source code can be downloaded from GitHub.

Let us create a record called “Person”

public record Person
    {
        public string FirstName { get; init; }
        public string LastName { get; init; }
    }

In this example, we've created a Person record type with two properties, FirstName and LastName. The init keyword makes sure these properties can only be set during object initialization, enforcing immutability.

Value-Based Equality

One of the most significant advantages of record types is their built-in value-based equality. Two record instances with the same property values are considered equal, simplifying comparisons.

using RecordTypes;

Console.WriteLine("Example of record in C#");

var person1 = new Person { FirstName = "Michael", LastName = "V" };
var person2 = new Person { FirstName = "Michael", LastName = "V" };

bool areEqual = person1 == person2; // true

Console.WriteLine($"Are person1 and person2 equal? {areEqual}");
Console.Read();

Behind the scenes, the C# compiler generates the necessary code for implementing the Equals() and GetHashCode() methods to achieve this behavior.

Deconstruction

Record types support deconstruction, which allows you to extract the values of the properties easily.

Let us take an example of “Employee”

public record Employee(string FirstName, string LastName);

In Program.cs

Console.WriteLine("Deconstruction example");

var employee = new Employee("Jane","Smith");
var (firstName, lastName) = employee;

Console.WriteLine($"First Name: {firstName}, Last Name: {lastName}");

Console.Read();

This feature can be especially useful when working with tuples or when you need to quickly access the properties of a record.

With Expressions

Another useful feature of record types is the expression, which allows you to create new record instances based on an existing one with some property values modified.

var employee2 = employee with { FirstName = "John" };

Console.WriteLine($"First Name: {employee2.FirstName}, Last Name: {employee2.LastName}");

Console.Read();

This allows you to maintain immutability while easily creating new objects with slight modifications.

Conclusion

Record types in C# provide a concise and efficient way to work with immutable data structures, thanks to their value-based equality, deconstruction, and expressions. They help reduce boilerplate code and make your codebase more expressive and maintainable. Consider using record types in your C# projects to take advantage of these benefits and write cleaner and more efficient code.

In this blog post, we've covered the basics of record types in C#, but there's more to explore, such as pattern matching and inheritance with records. So, dive deeper into this powerful feature and enhance your C# programming skills.