C# 9 Record Types - Inheritance and Equality Comparison Issue in Collections
I'm having trouble with Quick question that's been bugging me - I'm stuck on something that should probably be simple..... I'm encountering an issue with equality comparison when using C# 9 record types in a collection, specifically when I have a derived record that should override the equality comparison of its base record. In my situation, I created a base record `Person` and a derived record `Employee`. I expected that when I add instances of `Employee` to a list of `Person`, the equality checks based on property values would work seamlessly. However, I'm seeing unexpected behavior when checking for existence in the collection. Below is a simplified version of my code: ```csharp public record Person(string Name); public record Employee(string Name, string Position) : Person(Name); var people = new List<Person>(); people.Add(new Employee("John Doe", "Developer")); var exists = people.Contains(new Employee("John Doe", "Developer")); // This returns false ``` I expected `exists` to be `true`, since the employee's `Name` matches with the one in the list. I even tried implementing `IEqualityComparer<Person>` explicitly, but I still get `false`. Hereβs my implementation: ```csharp public class PersonComparer : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { return x.Name == y.Name; } public int GetHashCode(Person obj) { return obj.Name.GetHashCode(); } } var existsWithComparer = people.Contains(new Employee("John Doe", "Developer"), new PersonComparer()); // Still false ``` Iβve checked that the `Person` record's `Name` property is indeed what I expect it to be, and I've also tried using a different list that contains different kinds of records to see if the issue persists. Is there something I'm missing regarding the way equality is handled for record types with inheritance? Any help would be appreciated! For context: I'm using C# on Ubuntu. What's the best practice here? I'm working on a CLI tool that needs to handle this. Any ideas what could be causing this? I recently upgraded to C# stable. Am I approaching this the right way? Could this be a known issue?