C# 9.0 - Introduction To Record Types

Introduction

 
In this article, I am going to explain Record types which have been introduced in C# 9.0. I have split this concept into multiple articles. This is the first article and can be used by a beginner, intermediate, and professional.
 
Record type or record is a very interesting feature introduced in C# 9.0. As we know in F#, everything is considered as immutable, and similarly in C# record types help us to work with immutable types.
 
Prerequisites
  • .NET 5.0
  • Visual Studio 2019 (V 16.8, Preview 3)
What is Record Type OR Record?
 
Before we start on record type, you should have a basic understanding of Init – properties.
 
I suggest you go through this article first, if you are not aware about Init-Only Property.
 
Do you remember that Microsoft announced data class in a previous release? That data class is called record type in the new release. As the name suggests it is a record, not data.
 
Let's undestand the below concept first. In simple words,
  • Immutable means it cannot change. By default, Record types are immutable.
  • Record Type - it is a compact and easy way to write reference types (immutable) that automatically behave like value type.
 Let’s discuss the below code snippet,
  1. public class 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. }  
Init properties are immutable properties which means member class properties cannot change. What will you do if you want to add a new property in the Member class (like the middle name) or change the value of the address property? The only way is you have to create new objects and then assign a new value, right?
 
Here I am going to create a new Membar class object and assign values to it.
  1. var member = new Member  
  2. {  
  3.     Id=1,  
  4.     FirstName="Kirtesh",  
  5.     LastName="Shah",  
  6.     Address="Vadodara"  
  7. };  
Assume that you want to change the address from “Vadodara ” to “Mumbai”.
 
See the below code,
  1. var new member = new Member  
  2. {  
  3.     Id = member.Id,  
  4.     FirstName = member.FirstName,  
  5.     LastName = member.LastName,  
  6.     Address = "Mumbai"  
  7. };  
Now suppose we want to add new property, Middle Name, in Member class.
  1. public class Member  
  2. {  
  3.     public int ID { get; init; }  
  4.     public string FirstName { get; init; }  
  5.     public string MiddleName { get; init; }  
  6.     public string LastName { get; init; }  
  7.     public string Address { get; init; }  
  8. }  
  9.   
  10. var member = new Member  
  11. {  
  12.     Id=1,  
  13.     FirstName="Kirtesh",  
  14.     MiddleName = ”D”,  
  15.     LastName="Shah",  
  16.     Address = "Vadodara"  
  17. };  
  18.   
  19. var newMember = new Member  
  20. {  
  21.     Id = member.Id,  
  22.     FirstName = member.FirstName,  
  23.     MiddleName = member.MiddleName,  
  24.     LastName = member.LastName,  
  25.     Address = "Mumbai"  
  26. };  
What you will do if you have hundreds of properties? It will become so difficult, right? Hence C# 9.0 introduced Record type to work with immutable data and solve the above issue.
 

How to create Record Type?

 
Let’s try to implement Record Type in Member class. See the below code:
  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. }  
What’s the difference between the earlier Member class and the new Member record type? The only change is, we have replaced “Class” keyword with “Record”. Now Member record type is treated as an immutable data value.
 

With expression

 
C# 9.0 introduces the “With” expression with RecordType. It is mainly used to create new objects more effectively.
 
Before we understand the “With” expression let's discuss the below code,
  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. }  
  1. var member = new Member  
  2. {  
  3.     Id=1,  
  4.     FirstName="Kirtesh",  
  5.     LastName="Shah",  
  6.     Address = "Vadodara"  
  7. };  
  8.   
  9. var newMember = new Member  
  10. {  
  11.     Id = member.Id,  
  12.     FirstName = member.FirstName,  
  13.     LastName = member.LastName,  
  14.     Address = "Mumbai"  
  15. };  
Now we will use the “With” expression to write the above code. See the below code snippet,
  1. var member = new Member  
  2. {  
  3.     Id=1,  
  4.     FirstName="Kirtesh",  
  5.     LastName="Shah",  
  6.     Address = "Vadodara"  
  7. };  
  8.   
  9. var newMember = member with { Address = "Mumbai" };  
“With” expression is used as a {} syntax that allows you to define new values for a specific property. It will make development faster, easy, and clean.
 
That’s all for this article. I will explain more concepts in upcoming articles on Record Type.
 
Hope you enjoy this article and find it useful.