M
Matt
Hello,
In a few classes I have overridden Equals, ==, and != and would like to
generate an optimal hash code but I am a little confused about the best
approach to take. All of the classes that I am developing use a value
comparison, not reference, to determine equality. The classes were all
originally generated from an XSD, so there is a hierarchical
organization of classes. Because I am doing a value comparison, the
actual contents of the objects are relevant, but this gets tricky
because the majority of these classes contain arrays of additional
objects, which in turn contain more objects.
For example, one boardgame is only equivalent to another board game if
it contains the same number of game pieces, and those pieces are on the
same relative spots on the board, and the pieces are the same color,
and the pieces have the same stickers on them, and the stickers have
the same adhesive, etc.. You can see how it gets laborious to generate
a hash code based on the value of one's contents if it is composed of
objects which are composed of other objects and so on. The boardgame is
not the same if the adhesive on the game pieces is different, so the
hash codes should be different in that case, right? If the hash code
were based simply on the number of pieces, their location, and their
color then I could expect collisions and that would introduce all sorts
of confusion.
Would you just use Object.GetHashCode and forget about it... or would
you generate a hash code from an exhaustive dig into an object's
contents? I was thinking of generating a hashcode from the hash codes
of the member variables? What is the best approach for this issue?
Thanks
In a few classes I have overridden Equals, ==, and != and would like to
generate an optimal hash code but I am a little confused about the best
approach to take. All of the classes that I am developing use a value
comparison, not reference, to determine equality. The classes were all
originally generated from an XSD, so there is a hierarchical
organization of classes. Because I am doing a value comparison, the
actual contents of the objects are relevant, but this gets tricky
because the majority of these classes contain arrays of additional
objects, which in turn contain more objects.
For example, one boardgame is only equivalent to another board game if
it contains the same number of game pieces, and those pieces are on the
same relative spots on the board, and the pieces are the same color,
and the pieces have the same stickers on them, and the stickers have
the same adhesive, etc.. You can see how it gets laborious to generate
a hash code based on the value of one's contents if it is composed of
objects which are composed of other objects and so on. The boardgame is
not the same if the adhesive on the game pieces is different, so the
hash codes should be different in that case, right? If the hash code
were based simply on the number of pieces, their location, and their
color then I could expect collisions and that would introduce all sorts
of confusion.
Would you just use Object.GetHashCode and forget about it... or would
you generate a hash code from an exhaustive dig into an object's
contents? I was thinking of generating a hashcode from the hash codes
of the member variables? What is the best approach for this issue?
Thanks