In
such a case, the hash table may fail to find the bucket your key is in. For this reason, it is imperative
that you override GetHashCode() if you override Equals() for an object. In fact, if you
override Equals() and not GetHashCode(), the VB compiler will let you know about it with a
friendly warning.
GetHashCode() implementations should adhere to the following rules:
??? If, for two instances, x.Equals(y) is True, then x.GetHashCode() = y.GetHashCode().
??? Hash codes generated by GetHashCode() need not be unique.
??? GetHashCode() is not permitted to throw exceptions.
If two instances return the same hash code value, they must be further compared with
Equals() to determine if they??™re equivalent. Incidentally, if your GetHashCode method is efficient,
you can base the inequality code path of your Operator <> and Operator = implementations on
it, since different hash codes for objects of the same type imply inequality. Implementing the
operators this way can be more efficient in some cases, but it all depends on the efficiency of
CHAPTER 14 n VB 2008 BEST PRACTICES 350
your GetHashCode() implementation and the complexity of your Equals method. In some cases,
when using this technique, the calls to the operators could be less efficient than just calling
Equals(), but in other cases, they could be remarkably more efficient.
Pages:
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572