c# - IEqualityComparer using list of string as comparer -
i'm attempting setup iequalitycomparer uses list of string comparing property.
when using except , intersect in 2 lines of code below, records seen 'new' , none recognized 'old'.
list<exclusionrecordlite> newrecords = currentrecords.except(historicalrecords, new exclusionrecordlitecomparer()).tolist(); list<exclusionrecordlite> oldrecords = currentrecords.intersect(historicalrecords, new exclusionrecordlitecomparer()).tolist();
this iequalitycomparer class (words list)
public class recordcomparer : iequalitycomparer<record> { public bool equals(record x, record y) { if (object.referenceequals(x, y)) homecoming true; if (x == null || y == null) homecoming false; homecoming x.words.sequenceequal(y.words); } public int gethashcode(record obj) { homecoming new { obj.words }.gethashcode(); } }
your gethashcode
incorrect. utilize 1 this:
public override int gethashcode() { if(words == null) homecoming 0; unchecked { int hash = 19; foreach (var word in words) { hash = hash * 31 + (word == null ? 0 : word.gethashcode()); } homecoming hash; } }
to reply why collection not override gethashcode
uses object.gethashcode
returns unique value: why c# not implement gethashcode collections?
c# iequalitycomparer
No comments:
Post a Comment