我有很多List
比較,使用sequenceEqual
檢查:相等比較器使用通用
this.Details.SequenceEqual<EventDetail>(event.Details, new DetailsEqualityComparer());
既然這樣,我有很多的樣板,寫萬噸級很相似(除了爲類型參數Equals
和getHashCode
)命名aClassEqualityComparer
,anotherClassEqualityComparer
等等...
在這一點上,我已經在鑫卡特這種方式使用泛型重寫我的比較器:
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
}
}
問題是:據我所知在Equals
方法,因爲使用的等於是Object.Equals,我總是得到false
。
錯誤在哪裏?
你期望這個從默認的相等比較有什麼不同? –