c# - Collection as key in Dictionary -
i have dictionary this:
var dic = dictionary<collection<myobject>, string>
then add entry this:
dic.add(myobjcoll, "1");
where myobjcoll contains single entry
on second run try add entry dic myobjcoll has 2 entries (one of them same 1 in first run) , exception key exists.
what missing here? expect dictionary key collection , surely 2 collections different number of entries cannot identical.
edit: happened myobjcoll initialized outside foreach , during second iteration updated. thought new collection 2 entries old 1 entry collection additional entry added.
to elaborate bit on issue:
i have collection of product row objects. each product identification object consisting of collection of key value pairs:
collection<productkeyvaluepair> productidentification; myproductrow.productidentification = productidentification;
i have construct collection of productkeyvaluepairs defining "owner" of each product row object (one product row object can have multiple owners) , need add each owner correspoding procuct row object(s). traditional many many common key collection of keyvaluepairs.
hard explain it's down legacy data returned old systems running on mainframe , stored in hierarchical database of sort :-/
keys in dictionaries expected immutable while performing role key. if change value of gethashcode()
or equals(object)
undefined behavior.
(also if collection
not implement gethashcode()
, equals(object)
use members contains comparison reference container object itself)
Comments
Post a Comment