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,
Below is the normal person class
- namespace Record.Sample.Demo
- {
- public class ClassPerson
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public bool IsStudent { get; set; }
- }
- }
Now we will create an object of person class
- using static System.Console;
-
- namespace Record.Sample.Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- WriteLine("Class usage");
- ClassPerson classPersonA = new ClassPerson
- {
- Name = "Huli",
- Age = 20,
- IsStudent = true
- };
- WriteLine($"classPersonA Name: {classPersonA.Name}");
- WriteLine($"classPersonA Age: {classPersonA.Age}");
- WriteLine($"classPersonA IsStudent: {classPersonA.IsStudent}");
- WriteLine();
-
- ClassPerson classPersonB = new ClassPerson
- {
- Name = "Huli",
- Age = 20,
- IsStudent = true
- };
- WriteLine($"classPersonB Name: {classPersonB.Name}");
- WriteLine($"classPersonB Age: {classPersonB.Age}");
- WriteLine($"classPersonB IsStudent: {classPersonB.IsStudent}");
- WriteLine();
- WriteLine($"classPersonA Object Identification Value: {classPersonA.GetHashCode()}");
- WriteLine($"classPersonB Object Identification Value: {classPersonB.GetHashCode()}");
- WriteLine($"classPersonA and classPersonB identfication are not same");
-
- ReadLine();
- }
- }
- }
Below is the output snap of class usage
Below is the person record (looks like class, instead of class we just need to write record)
- namespace Record.Sample.Demo
- {
- public record RecordPerson
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public bool IsStudent { get; set; }
- }
- }
Now we will create an object of person record
- using static System.Console;
-
- namespace Record.Sample.Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- WriteLine("Record usage");
- RecordPerson RecordPersonA = new RecordPerson
- {
- Name = "Huli",
- Age = 20,
- IsStudent = true
- };
- WriteLine($"RecordPersonA Name: {RecordPersonA.Name}");
- WriteLine($"RecordPersonA Age: {RecordPersonA.Age}");
- WriteLine($"RecordPersonA IsStudent: {RecordPersonA.IsStudent}");
- WriteLine();
-
- RecordPerson RecordPersonB = new RecordPerson
- {
- Name = "Huli",
- Age = 20,
- IsStudent = true
- };
- WriteLine($"RecordPersonB Name: {RecordPersonB.Name}");
- WriteLine($"RecordPersonB Age: {RecordPersonB.Age}");
- WriteLine($"RecordPersonB IsStudent: {RecordPersonB.IsStudent}");
- WriteLine();
- WriteLine($"RecordPersonA Object Identification Value: {RecordPersonA.GetHashCode()}");
- WriteLine($"RecordPersonB Object Identification Value: {RecordPersonB.GetHashCode()}");
- WriteLine($"RecordPersonA and RecordPersonB identfication are same");
-
- ReadLine();
- }
- }
- }
Below is the output snap of record usage
Below is the comparison image of class and record usage.
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.