Z
Zachary Turner
I want to use a custom object as a key in a Dictionary or SortedList,
and uniqueness is determined by more than just a specific field. I
know the "suggested" method is to xor all the hash codes together, but
this doesn't seem safe? For example, say
Key1 = (A, B)
Key2 = (C, D)
Is it possible to have the following situation?
A != C
B != D
A.GetHashCode() ^ B.GetHashCode() == C.GetHashCode() ^ D.GetHashCode()
Actually, I take that back. I know it's possible. For example, 1 ^ 2
== 5 ^ 6. But how serious of a problem is this in reality? There must
be some mitigating factor here that I'm missing, otherwise why would
examples of using XOR be strewn throughout the MSDN documentation?
Thanks for any insight.
and uniqueness is determined by more than just a specific field. I
know the "suggested" method is to xor all the hash codes together, but
this doesn't seem safe? For example, say
Key1 = (A, B)
Key2 = (C, D)
Is it possible to have the following situation?
A != C
B != D
A.GetHashCode() ^ B.GetHashCode() == C.GetHashCode() ^ D.GetHashCode()
Actually, I take that back. I know it's possible. For example, 1 ^ 2
== 5 ^ 6. But how serious of a problem is this in reality? There must
be some mitigating factor here that I'm missing, otherwise why would
examples of using XOR be strewn throughout the MSDN documentation?
Thanks for any insight.