I have an array of CustomObject
s and a custom IEqualityComparer<CustomObject>
. I have hard coded the IEqualityComparer.GetHashCode()
method to return a constant 42
.
When I run linq's Distinct
method on the array, nothing is filtered out. Anyone know why?
Note: I know there are a number of questions on here about this issue, however, the ones I've seen (C# Distinct on IEnumerable<T> with custom IEqualityComparer, Distinct() with lambda?, etc) only say to make sure to implement GetHashCode
. None of them explain, why it doesn't work.
Code:
public class CustomObject
{
public string Name { get; set; }
}
public class CustomObjectEqualityComparer : IEqualityComparer<CustomObject>
{
public bool Equals(CustomObject x, CustomObject y)
{
//Every CustomObject should now be 'equal'!
return x.GetHashCode() == y.GetHashCode();
}
public int GetHashCode(CustomObject obj)
{
//Every CustomObject should now be 'equal'!
return 42;
}
}
Test:
[TestFixture]
public class TestRunner
{
private CustomObject[] customObjects =
{
new CustomObject {Name = "Please"},
new CustomObject {Name = "Help"},
new CustomObject {Name = "Me"},
new CustomObject {Name = "Stack"},
new CustomObject {Name = "Overflow"},
};
[Test]
public void DistinctTest()
{
var distinctArray =
customObjects.Distinct(new CustomObjectEqualityComparer()).ToArray();
//Since every CustomObject is 'Equal' there should only be
//1 element in the array.
Assert.AreEqual(1, distinctArray.Length);
}
}
This is the output I get from running the test:
Expected: 5
But was: 1
If I debug the test, I can see that GetHashCode
is being called, so why isn't Distinct
filtering out all the 'duplicates'?