Get HashCode

The GetHashCode() method is used to generate a hash code for value types and classes that need one. Without getting too technical, a hash code is an integer that can be used to identify an object. They are used when storing objects in data structures, such as hash tables.

It is obvious what the hash code ought to be for some classes: For all my bank accounts, the account number is a unique integer value, so that will do very nicely. For other classes it isn't as obvious: What should the hash code of a string be?

GetHashCode() provides a way for class implementers to generate a suitable hash code for their classes, and it must be overridden for value types.

0 0

Post a comment