it’s 10:25 PM IST and as usual I was doing some learning on C# 10 features and suddenly I got diverted to play around with equality comparison of record types with their member’s value unlike a class. I must tell you here, I was surprised to see the results and reach to conclusion of using this very very carefully or simply ignore until really needed to go for record types.
In case you landed newly here: record type is a new type introduced on C# 9 and enhanced in C#10. For details on C# 9 features please visit C# 9.0 features : a developer handbook
Let’s understand this with below example. To carry on with examples I defined three record types as below:
public record Person(string FirstName, string LastName, string[] PhoneNumbers);
public record Teacher(string FirstName, string LastName, string[] PhoneNumbers, int Grade)
: Person(FirstName, LastName, PhoneNumbers);
public record Student(string FirstName, string LastName, string[] PhoneNumbers, int Grade)
: Person(FirstName, LastName, PhoneNumbers);
Example 1: Results True — Happy path
var phoneNumbers = new string[2];
var teacher1 = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
var teacher2 = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
Console.WriteLine(teacher1 == teacher2);//true
Example 2: Results False — Oops record type spotted two difference string array for Phone numbers.
var phoneNumbers = new string[2];
var teacher3 = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
var teacher4 = new Teacher("Nancy", "Davolio", new string[2], 3);
Console.WriteLine(teacher3 == teacher4);
Example 3: Results False — Aah really, record type don’t entertain the reference types to check even they have equal values in it.
var teacher5 = new Teacher("Nancy", "Davolio", new string[2] { "555-1234", "555-6789" }, 3);
var teacher6 = new Teacher("Nancy", "Davolio", new string[2] { "555-1234", "555-6789" }, 3);
Console.WriteLine(teacher5 == teacher6);
Example 4: Results True— BOOM! though there is a reference type for phone numbers but they points to same memory location or both memory location as well as value, yeah this is the case.
I would have simply memorize it if it would have been part of my academic syllabus.
var phoneNumbers = new string[2] { "555-1234", "555-6789" };
var teacher7 = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
var teacher8 = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
Console.WriteLine(teacher7 == teacher8);
So the conclusion here is, if you want to use record types because of their nature then be extra cautious to save yourself from Hell considering you are going to enjoy Heaven. :)
Now lets see how it behaves in case of inheritance hierarchies. Here we go:
Example 1: Results False. Aah, they can be equal only if their run-time type is equal not because they belongs to same parent.
var phoneNumbers = new string[2];
var teacher = new Teacher("Nancy", "Davolio", phoneNumbers, 3);
var student = new Student("Nancy", "Davolio", phoneNumbers, 3);
Console.WriteLine(teacher == student);//false
Example 2: Results True. Because they are really twins from same father, kidding :)
var phoneNumbers = new string[2];
var student1 = new Student("Nancy", "Davolio", phoneNumbers, 3);
var student2 = new Student("Nancy", "Davolio", phoneNumbers, 3);
Console.WriteLine(student1 == student2);//true
So the conclusion here is, For two record variables to be equal, the run-time type must be equal and store the same values.
Thank You for reading. Hope you enjoyed and don’t forget to fuel me up for such articles with your claps as much you can.
No comments:
Post a Comment