I have a lot of List
comparison, checked using sequenceEqual
:
this.Details.SequenceEqual<EventDetail>(event.Details, new DetailsEqualityComparer());
Since in this way I have a lot of boilerplate, writing tons of class pretty similar (except for the type parameter for Equals
and getHashCode
) named aClassEqualityComparer
, anotherClassEqualityComparer
and so on...
At this point, i have thinked to rewrite my comparer using generics in this way:
class GenericEqualityComparer<T> : IEqualityComparer<T> where T : class
{
public bool Equals(T x, T y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.Equals(y); // here comes the problems
}
public int GetHashCode(T obj)
{
// some hashing calculation
}
}
The problem is: as far as I suppose in the Equals
method, since the used Equals is the Object.Equals and I always get false
.
Where is the error?