Although we are not going to place our Person into a System.Collections.Hashtable, for com-
pletion, let’s override GetHashCode(). There are many algorithms that can be used to create a hash
code, some fancy, others not so fancy. Most of the time, you are able to generate a hash code value
by leveraging the System.String’s GetHashCode() implementation.
Given that the String class already has a solid hash code algorithm that is using the character
data of the String to compute a hash value, if you can identify a piece of field data on your class that
should be unique for all instances (such as the Social Security number), simply call GetHashCode()
on that point of field data. If this is not the case, but you have overridden ToString(), call
GetHashCode() on your own string representation:
Do'stlaringiz bilan baham: