Compare Record and Class in C# 9

Compare Record and Class in C# 9

In C#9, A record is a type of class that is intended to work with immutable (read-only) data. The non-destructive mutation is its most useful feature:

The syntax for creating the Record

public record RPerson(string FirstName,string LastName);

If you want to create a class which have the same behaviour as the above statement, then you can achieve by using class as shown below ( its not complete source code; there is a lot when Record is converted to C#){alertInfo}

public class CPerson{
	public string FirstName { get; init; }
	public string LastName { get; init; }
	public CPerson(string firstName,string lastName)

Let’s Compare the difference between class and Record with an example

void Main()
	var rPerson1=new RPerson("John","Doe");
	var rPerson2=new RPerson("John","Doe");

	var cPerson1 = new RPerson("John", "Doe");
	var cPerson2 = new RPerson("John", "Doe");


You can eliminate almost all boilerplate with positional parameters. This makes records useful for simple types that combine or hold data.{alertInfo}

Record by Default provide structural equality

Record by default override Object.Equal method to compare two Record. Suppose Record have the same value, then its return true otherwise false.

See the following example.

var cPerson1 = new CPerson("John", "Doe");
var cPerson2 = new CPerson("John", "Doe");
Console.WriteLine(rPerson1.Equals(rPerson2)); // True
Console.WriteLine(cPerson1.Equals(cPerson2)); // False

Note: C# compare record type by value, not by reference. If you try to compare their references, both will be different.

Record By Default override GetHashCode

Record override the GetHashCode method internally.
If you compare two records with the same value there, the hash code will be the same. see the below example

Console.WriteLine(rPerson1.GetHashCode()==rPerson2.GetHashCode()); // True
Console.WriteLine(cPerson1.GetHashCode()==cPerson2.GetHashCode()); // False

Record by Default override ToString

Record provide override version of ToString, which print the RecordRecord in the friendly format while class print the name.

Console.WriteLine(rPerson1.ToString()); // RPerson { FirstName = John, LastName = Doe }
Console.WriteLine(cPerson1.ToString()); // CPerson

Record by default override == and !=

The RecordRecord already provide operator overloading for == and !=, so it easy to compare two records.

Console.WriteLine(rPerson1==rPerson2); // True
Console.WriteLine(cPerson1==cPerson2); // False

Copy Record with with

C# 9 automatically defines a protected ‘copy constructor, which copies the underlying fields from another record.

	var rPersonCopy = rPerson1 with {
		FirstName="Updated Name"
	Console.WriteLine(rPersonCopy); // RPerson { FirstName = Updated Name, LastName = Doe }
	Console.WriteLine(rPerson1==rPersonCopy); // False
  • The with keyword performs non-destructive mutation:
  • rPersonCopy is a new record; we haven’t altered the original person.

When to use Record?

  • Loading external data from API or Database that does; not change.
  • Thread Safe
  • When you are Processing Huge Data
  • When you want to store Read-only data

When not to use RecordRecord

When you need to change the data like database operations

Other features

  • The record type can only inherit from another record, not from class
  • By default, the Record type is immutable, but you can create mutable Record but not recommended
public record Person { 
 public string FirstName {get;set;}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post