Record In C# 9.0

Introduction

 
Record is a new keyword added in C# 9.0 version. Records are similar to classes. There is a difference in the way it saves data.
 
Records seem to be very useful when we want to ignore duplicated data or records.
 
We will understand this with an example.
 
To use the record we should use .Net 5.0 Framework,
 
Record In C# 9.0
 
Below is the normal person class
  1. namespace Record.Sample.Demo  
  2. {  
  3.     public class ClassPerson  
  4.     {  
  5.         public string Name { getset; }  
  6.         public int Age { getset; }  
  7.         public bool IsStudent { getset; }  
  8.     }  
  9. }  
Now we will create an object of person class
  1. using static System.Console;  
  2.   
  3. namespace Record.Sample.Demo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             WriteLine("Class usage");  
  10.             ClassPerson classPersonA = new ClassPerson  
  11.             {  
  12.                 Name = "Huli",  
  13.                 Age = 20,  
  14.                 IsStudent = true  
  15.             };  
  16.             WriteLine($"classPersonA Name: {classPersonA.Name}");  
  17.             WriteLine($"classPersonA Age: {classPersonA.Age}");  
  18.             WriteLine($"classPersonA IsStudent: {classPersonA.IsStudent}");  
  19.             WriteLine();  
  20.   
  21.             ClassPerson classPersonB = new ClassPerson  
  22.             {  
  23.                 Name = "Huli",  
  24.                 Age = 20,  
  25.                 IsStudent = true  
  26.             };  
  27.             WriteLine($"classPersonB Name: {classPersonB.Name}");  
  28.             WriteLine($"classPersonB Age: {classPersonB.Age}");  
  29.             WriteLine($"classPersonB IsStudent: {classPersonB.IsStudent}");  
  30.             WriteLine();  
  31.             WriteLine($"classPersonA Object Identification Value: {classPersonA.GetHashCode()}");  
  32.             WriteLine($"classPersonB Object Identification Value: {classPersonB.GetHashCode()}");  
  33.             WriteLine($"classPersonA and classPersonB identfication are not same");  
  34.              
  35.             ReadLine();  
  36.         }  
  37.     }  
  38. }   
Below is the output snap of class usage
 
Record In C# 9.0
 
Below is the person record (looks like class, instead of class we just need to write record)
  1. namespace Record.Sample.Demo  
  2. {  
  3.     public record RecordPerson  
  4.     {  
  5.         public string Name { getset; }  
  6.         public int Age { getset; }  
  7.         public bool IsStudent { getset; }  
  8.     }  
  9. }  
Now we will create an object of person record
  1. using static System.Console;  
  2.   
  3. namespace Record.Sample.Demo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {        
  9.             WriteLine("Record usage");  
  10.             RecordPerson RecordPersonA = new RecordPerson  
  11.             {  
  12.                 Name = "Huli",  
  13.                 Age = 20,  
  14.                 IsStudent = true  
  15.             };  
  16.             WriteLine($"RecordPersonA Name: {RecordPersonA.Name}");  
  17.             WriteLine($"RecordPersonA Age: {RecordPersonA.Age}");  
  18.             WriteLine($"RecordPersonA IsStudent: {RecordPersonA.IsStudent}");  
  19.             WriteLine();  
  20.   
  21.             RecordPerson RecordPersonB = new RecordPerson  
  22.             {  
  23.                 Name = "Huli",  
  24.                 Age = 20,  
  25.                 IsStudent = true  
  26.             };              
  27.             WriteLine($"RecordPersonB Name: {RecordPersonB.Name}");  
  28.             WriteLine($"RecordPersonB Age: {RecordPersonB.Age}");  
  29.             WriteLine($"RecordPersonB IsStudent: {RecordPersonB.IsStudent}");  
  30.             WriteLine();  
  31.             WriteLine($"RecordPersonA Object Identification Value: {RecordPersonA.GetHashCode()}");  
  32.             WriteLine($"RecordPersonB Object Identification Value: {RecordPersonB.GetHashCode()}");  
  33.             WriteLine($"RecordPersonA and RecordPersonB identfication are same");  
  34.   
  35.             ReadLine();  
  36.         }  
  37.     }  
  38. }   
Below is the output snap of record usage
 
Record In C# 9.0
 
Below is the comparison image of class and record usage.
 
Record In C# 9.0
 
In the above image we can see that the object identifier is different when we use class, even all the properties of class have the same value. But in record it is not like this, we even created 2 instance and as properties are the same, records are  pointing to the same identification. This is the main difference between record and class. Record is best here for in memory management in this case.
 
So depending on our need/requirement, we need to make a decision as to whether we need to use class or record. Both are best in their own place.
 

Summary

 
In this article welearned about the usage of record and when we can use it. I hope it helps. The solution is attached to this article. You can download, run and see the output in Visual Studio 2019.


Similar Articles