Using Records In C# 9.0

Introduction

In today’s article, we will look at records. This is a new feature in C# 9.0. C# 9.0 has been introduced with .NET 5.0. We will look at what records are, how they work, the advantages of having records, and how they differ from classes and structs.

What are Records?

Microsoft recently announced the availability of a .NET 5 release candidate at Microsoft Ignite 2020. This included the latest features in C# 9.0. In order to code in .NET 5.0, we would need to install the latest preview of Visual Studio 2019 (Version 16.8.0 Preview 3.1). As I had read about some cool features in C# 9.0, including my favorite “ Records”, I downloaded and installed the required version as below.

Using Records In C# 9.0

What exactly are records?

Well, to understand this let us see what the difference between classes and structs. As we know, classes are passed around by reference, whereas for structs, we need to make a copy to pass it around. However, for structs, comparisons for equality are based on values, whereas for classes, comparisons for equality are based on the reference the class is pointing to. Hence, two classes might have the exact same content, but as their references are different, they will not be considered equal. We would have to implement custom code for comparing elements in a class. This is where records come in and give us the best of both worlds. They are immutable, can be passed around by reference, and are compared based on their contents. Let us see them in action.

Let us create a console application in Visual Studio 2019 (Version 16.8.0 Preview 3.1) as below.

 Visual Studio 2019

creat new project

Using Records In C# 9.0

Next, we will change the properties of the project and set the Target Framework to .NET 5.0, as below.

 Framework to .NET 5.0

Now add the code below.

using System;
namespace ConsoleAppRecords
{
    public class Program
    {
        static void Main(string[] args)
        {
            // Classes
            var employeeClass = new EmployeeClass();
            employeeClass.Id = 1;
            employeeClass.EmpName = "John Smith";
            employeeClass.Salary = 1000;
            var secondEmployeeClass = new EmployeeClass();
            secondEmployeeClass.Id = 1;
            secondEmployeeClass.EmpName = "John Smith";
            secondEmployeeClass.Salary = 1000;
            Console.WriteLine(employeeClass == secondEmployeeClass
                ? "employeeClass is equal to secondEmployeeClass"
                : "employeeClass is not equal to secondEmployeeClass");

            // Records
            var employeeRecord = new Employee();
            employeeRecord.Id = 1;
            employeeRecord.EmpName = "John Smith";
            employeeRecord.Salary = 1000;
            var secondEmployeeRecord = new Employee();
            secondEmployeeRecord.Id = 1;
            secondEmployeeRecord.EmpName = "John Smith";
            secondEmployeeRecord.Salary = 1000;
            var thirdEmployeeRecord = new Employee();
            thirdEmployeeRecord.Id = 2;
            thirdEmployeeRecord.EmpName = "John Smith";
            thirdEmployeeRecord.Salary = 1000;
            Console.WriteLine(employeeRecord == secondEmployeeRecord
                ? "employeeRecord is equal to secondEmployeeRecord"
                : "employeeRecord is not equal to secondEmployeeRecord");
            Console.WriteLine(employeeRecord == thirdEmployeeRecord
                ? "employeeRecord is equal to thirdEmployeeRecord"
                : "employeeRecord is not equal to thirdEmployeeRecord");
            // Updating Records
            var updatedEmployee = employeeRecord with
            {
                Salary = 2000
            };
            Console.WriteLine(employeeRecord == updatedEmployee
                ? "employeeRecord is equal to updatedEmployee"
                : "employeeRecord is not equal to updatedEmployee");

            Console.ReadKey();
        }
    }
    public record Employee
    {
        public int Id { get; set; }
        public string EmpName { get; set; }
        public double Salary { get; set; }
    }
    public class EmployeeClass
    {
        public int Id { get; set; }
        public string EmpName { get; set; }
        public double Salary { get; set; }
    }
}

In the above code, we first create two classes with the same values for each member. However, these will not be returned as equal as they point to separate references.

Then, we create two records in which the members have the same values. These will be returned as equal. For further testing, we create a third record that has a member (Id) with a different value. Now, when we compare this record with the first one, they are considered different as the values of the members are not the same.

Finally, we see how to create a new record from an existing one with a value of one member changed. This will create a new record, and the old one will have the same previous value. We can then compare and see that they are considered different.

We can execute the code and confirm our results.

code result

Summary

In this article, we looked at records which are a new feature in C# 9.0. The main advantage of using records is that they are immutable and, when passed in a state to some function, are guaranteed to stay in the same state. Happy Coding.


Similar Articles