c# - Hashset not adding a duplicate, but returning true for Add() -
(edit) more info. first notice "new virtual". class inherits base class supposed generic parent-aware class can created icollection type. here's descriptor, basically:
public abstract class parentawarecollection<tobject, tparent, tinnerlist> : icollection<tobject>, icollection, iparentprovider<tparent> tobject : iparentprovider<tparent> tinnerlist : icollection<tobject>, new() { protected tinnerlist innerlist = new tinnerlist(); ... }
this class:
public class parentawarehashset<tobject, tparent> : parentawarecollection<tobject, tparent, hashset<tobject>>, iset<tobject>, icollection tobject : iparentprovider<tparent> { public parentawarehashset(tparent parent) : base(parent)
iparentprovider requires "parentcollection" object present.
in debugging, overridden (void) add method in parentawarecollection
never called. don't think "new" problem.
also, here's don't understand. here's descriptor actual hashset:
public class hashset<t> : iserializable, ideserializationcallback, iset<t>, icollection<t>, ienumerable<t>, ienumerable { ... public bool add(t item); }
notice in implements icollection mine. led me believe ok do. unlike mine, framework hashset not use new
descriptor add() method, that's required because icollection<t>
implements void add(t item)
. perhaps omitted it?
original question:
i have collection class uses hashset<t>
store objects. objects of type t override gethashcode()
, in make sure required information present used produce hash code. not sure if important.
what happens when add(t) hashset, not add object, returns true. if debug , in immediate window try add again, returns false, it's supposed to. method looks this:
public new virtual bool add(tobject item) { // must add parent first, since may used in hash code // innerlist hashset<t> if (innerlist.any(existing=>item.gethashcode()==existing.gethashcode())) { return(false); } else { if (innerlist.add(item)) { return(true); } else { return(false); } } }
i added first condition make code work. works expected there. however, can't understand why have this, , can think of no reason, ever, why hashset return "true" add() without adding anything. if gethashcode override messed up, should either add , return true, or not , return false. thoughts?
here's observed while debugging:
breaking before innerlist.add():
?innerlist.count 1 ?item.gethashcode() -1629834529 ?innerlist.elementat(0).gethashcode() -1629834529
step on innerlist.add(), returns true:
?innerlist.count 1 ?innerlist.add(item) false
wtf? .net 4.0 framework.
your add
code wrong:
public new virtual bool add(tobject item) { // must add parent first, since may used in hash code // innerlist hashset<t> if (innerlist.any(existing=>item.gethashcode()==existing.gethashcode())) { return(false); } else { if (innerlist.add(item)) { return(true); } else { return(false); } } }
two different items can have same hash code! function should this:
public new virtual bool add(tobject item) { return innerlist.add(item); }
Comments
Post a Comment