G
Guest
I have some business objects which overrides Equals to provide syntax
equality instead of just reference equality. Overriding equals gives me a
warning that i must override GetHashcode(). Msdn says this about implementing
hashcode:
(1)If two objects of the same type represent the same value, the hash
function must return the same constant value for either object.
(2)For the best performance, a hash function must generate a random
distribution for all input.
(3)The hash function must return exactly the same value regardless of any
changes that are made to the object.
Since object.GetHashCode() is implemented using the object SYNCBLOC (i
think), it fall short when syntax equality is introduced (see req nr 1). The
problem is that i can't really se how i can combine req 1 and 3 if the object
is not immutable. Req 3 (as i interpret it) basically states that the
hashcode must be calculated based on member variables when the object is
initialized and it should not reflect changes to the object data. Now
consider the following: two objects are initialised with different values and
their hashes are calculated. We change the values of one of the objects so
that both objects are syntactically equal. Since the hashcodes we calculated
were based on the initialized values, req 1 is breached and the new
implementation is as bad as the original. Is there a way to implement
GetHashcode() correctly for mutable syntactically equal reference types?
Perhaps i am misinterpreting the implementation guidelines?
equality instead of just reference equality. Overriding equals gives me a
warning that i must override GetHashcode(). Msdn says this about implementing
hashcode:
(1)If two objects of the same type represent the same value, the hash
function must return the same constant value for either object.
(2)For the best performance, a hash function must generate a random
distribution for all input.
(3)The hash function must return exactly the same value regardless of any
changes that are made to the object.
Since object.GetHashCode() is implemented using the object SYNCBLOC (i
think), it fall short when syntax equality is introduced (see req nr 1). The
problem is that i can't really se how i can combine req 1 and 3 if the object
is not immutable. Req 3 (as i interpret it) basically states that the
hashcode must be calculated based on member variables when the object is
initialized and it should not reflect changes to the object data. Now
consider the following: two objects are initialised with different values and
their hashes are calculated. We change the values of one of the objects so
that both objects are syntactically equal. Since the hashcodes we calculated
were based on the initialized values, req 1 is breached and the new
implementation is as bad as the original. Is there a way to implement
GetHashcode() correctly for mutable syntactically equal reference types?
Perhaps i am misinterpreting the implementation guidelines?