C# 9.0 - Record Types

Introduction

 
This is the second article of C# 9.0- Record types. In the first article, we had discussed the introduction of Record type and "with" expression. To get a better understanding of this article, please go through 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 Recordtypes compare by their properties values and not by their memory location. This is also one of the important features of Record Type.
 
You might think, why Record type equalities 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:
  1. public record Member    
  2. {    
  3.     public int ID { get; init; }    
  4.     public string FirstName { get; init; }    
  5.     public string LastName { get; init; }    
  6.     public string Address { get; init; }    
  7. }    
Create an object of the Member record:
  1. var member = new Member    
  2. {    
  3.     Id=1,    
  4.     FirstName="Kirtesh",    
  5.     LastName="Shah",    
  6.     Address = "Vadodara"    
  7. };    
Now will create another with new values, 
  1. var newMember = member with { Address = "Mumbai" };  
 Now we will do the comparison, 
  1. var member = new Member    
  2. {    
  3.     Id=1,    
  4.     FirstName="Kirtesh",    
  5.     LastName="Shah",    
  6.     Address = "Vadodara"    
  7. };    
  8. Console.WriteLine(member == newMember);  
Will get result - false because the property values are different.
 
Now we will change the Address value "Vadodara" and make comparisons. This time we should get result - true as the first object and third object properties values will be the same. 
  1. var latestmember= newmember with { Address = "vadodara" };   
  2.   
  3. Console.WriteLine(member == latestmember);    
"Equals" and "==" compare all properties values. This is a very powerful feature in C# 9.0 Record type.
 

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.
  1. public record Member  
  2. {  
  3.     public string FirstName { get; init; }  
  4.     public string MiddleName { get; init; }  
  5.     public string LastName { get; init; }  
  6.   
  7.     public Member(string firstName, string middleName, string lastName)  
  8.     {  
  9.         FirstName = firstName;  
  10.         MiddleName = middleName;  
  11.         LastName = lastName;  
  12.     }  
  13. }  
Now will use this constructor in object creation:
  1. 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.
 
Hope you enjoyed this article and find it useful.