implementing implementing a custom IEqualityComparer in C# 10 causing incorrect hash code calculations
Quick question that's been bugging me - I'm working with an scenario with my custom implementation of `IEqualityComparer<T>` in C# 10 that seems to be causing incorrect hash code calculations. I have a class called `Person` with properties `Id` and `Name`, and I'm trying to compare instances based on `Id`. However, when I use my comparer in a `HashSet<Person>`, I notice that duplicates are being allowed, which shouldn't happen. Here is the code for my `Person` class and the custom comparer: ```csharp public class Person { public int Id { get; set; } public string Name { get; set; } } public class PersonComparer : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { if (x == null || y == null) return false; return x.Id == y.Id; } public int GetHashCode(Person obj) { if (obj == null) return 0; return obj.Id.GetHashCode(); } } ``` I then instantiate the `HashSet` like this: ```csharp var people = new HashSet<Person>(new PersonComparer()); people.Add(new Person { Id = 1, Name = "Alice" }); people.Add(new Person { Id = 1, Name = "Bob" }); // This should be rejected ``` To my surprise, the second `Add` call does not throw an exception, and both `Person` objects seem to be added to the set. I've checked that `Id` is unique for each `Person`, but it seems that the `GetHashCode` method might not be behaving as expected. I've also confirmed that `Id` is indeed the same for both instances, yet they are treated as distinct in the `HashSet`. I've tried overriding the `GetHashCode` method to simply return a constant value (like 0) just to check if the comparisons work properly, but that led to a `HashSet` that only contains one of the entries. So, I'm not sure where I'm going wrong. Any insights on why this is happening and how I can fix this scenario would be greatly appreciated! This is part of a larger service I'm building. Is there a better approach? I recently upgraded to C# 3.9. Am I missing something obvious? I'm on Windows 11 using the latest version of C#. Any examples would be super helpful.