C# 9.0 - Record Types

Introduction

This is the second article of C# 9.0- Record types. In the first article, we discussed the introduction of Record type and "with" expression. To get a better understanding of this article, please go through the first article from here.

This article can be used by any beginner, intermediate, and professional.

In this article, we are going to discuss.

  1. Record type equality.
  2. Constructor with Record type.

Prerequisites

  • .NET 5.0
  • Visual Studio 2019 (V 16.8, Preview 3)

Record type Equality

As we know, C# has two types.

  1. Value type:  Structure is a Value type
  2. Reference type:  Class is a Reference type.

If you want to understand Reference Type and Value Type equalities, please go through this article.

RecordType is a reference type like class and not a value type (like structure). But Record type equity is work like structure, I mean like a value type. Two record types are compared by their property values and not by their memory location. This is also one of the important features of Record Type.

why do Record type equities work like Value-type equities?

The answer is compiler generates the “Equals” method and also generates operator overloads “==” and “!=” for you.

Let’s discuss the below code snippet.

public record Member
{
    public int ID { get; init; }
    public string FirstName { get; init; }
    public string LastName { get; init; }
    public string Address { get; init; }
}

Create an object of the Member record.

var member = new Member
{
    ID = 1,
    FirstName = "Kirtesh",
    LastName = "Shah",
    Address = "Vadodara"
};

Now will create another with new values.

var newMember = member with { Address = "Mumbai" };

Now we will do a comparison.

var member = new Member
{
    ID = 1,
    FirstName = "Kirtesh",
    LastName = "Shah",
    Address = "Vadodara"
};
Console.WriteLine(member == newMember);

Will get the result false because the property values are different.

Now we will change the Address value to "Vadodara" and make comparisons. This time we should get a result - true as the first object and third object properties values will be the same.

var latestMember = newMember with { Address = "Vadodara" };
Console.WriteLine(member == latestMember);

"Equals" and "==" compare all property values. This is a very powerful feature in C# 9.0 Record type.

What is a Constructor?

Do you remember how the constructor was with the class while we initiated the object? Similarly, we can work with the record type.

Suppose you want to use the constructor with your record type. We can use the below code.

public record Member
{
    public string FirstName { get; init; }
    public string MiddleName { get; init; }
    public string LastName { get; init; }
    public Member(string firstName, string middleName, string lastName)
    {
        FirstName = firstName;
        MiddleName = middleName;
        LastName = lastName;
    }
}

Now will use this constructor in object creation.

var member = new Member("Kirtesh", "D", "Shah"); // constructor

That’s all for this article. I will explain more concepts in upcoming articles on Record Type.

I hope you enjoyed this article and find it useful.


Similar Articles