c# - Weird dictionary ContainsKey issue -
before start, i'd clarify not other "similar" questions out there. i've tried implementing each approach, phenomena getting here weird.
i have dictionary containskey
returns false
, if gethashcode
functions return same output, , if equals
method returns true
.
what mean? doing wrong here?
additional information
the 2 elements inserting both of type owner
, no gethashcode
or equals
method. these inherit type storable
, implements interface, , has gethashcode
, equals
defined.
here's storable
class. wondering if 2 guid
properties indeed equal - , yes, are. double-checked. see sample code afterwards.
public abstract class storable : istorable { public override int gethashcode() { return id == default(guid) ? 0 : id.gethashcode(); } public override bool equals(object obj) { var other = obj storable; return other != null && (other.id == id || referenceequals(obj, this)); } public guid id { get; set; } protected storable() { id = guid.newguid(); } }
now, here's relevant part of code dictionary stuff occurs. takes in supporter
object has link owner
.
public class chatsession : storable, ichatsession { static chatsession() { pendingsupportsessions = new dictionary<iowner, linkedlist<ichatsession>>(); } private static readonly idictionary<iowner, linkedlist<ichatsession>> pendingsupportsessions; public static chatsession assignsupporterfornextpendingsession(isupporter supporter) { var owner = supporter.owner; if (!pendingsupportsessions.containskey(owner)) //always returns false { var hashcode1 = owner.gethashcode(); var hashcode2 = pendingsupportsessions.first().key.gethashcode(); var equals = owner.equals(pendingsupportsessions.first().key); //here, equals true, , 2 hashcodes identical, //and there 1 element in dictionary according debugger. //however, calling 2 "add" calls after eachother indeed crash. pendingsupportsessions.add(owner, new linkedlist<ichatsession>()); pendingsupportsessions.add(owner, new linkedlist<ichatsession>()); //crash } ... } }
if need additional information, let me know. not sure kind of information sufficient, hard me include more.
guillaume right. appears changing value of 1 of keys after added dictionary. doh!
Comments
Post a Comment