For example, consider
an object that models a multidimensional point in space. Suppose the number of dimensions
(rank) of this point could easily approach into the hundreds. Internally, you could represent
the dimensions of the point by using an array of integers. Say you want to implement the
GetHashCode method by computing a 32-bit cyclic redundancy check (CRC32) on the dimension
points in the array. This also implies that this Point type is immutable. The GetHashCode() call
could potentially be expensive if you compute the CRC32 each time it is called. Therefore, it may
be wise to precompute the hash and store it in the object. In such a case, you could write the
equality operators as shown in the following code:
Public NotInheritable Class Point
Private coordinates As Single()
Private precomputedHash As Integer
'Other methods removed for clarity.
Public Overrides Function Equals(ByVal other As Object) As Boolean
Dim result As Boolean = False
Dim that As Point = TryCast(other, Point)
If Not that Is Nothing Then
result = (Me.coordinates Is that.coordinates)
End If
Return result
End Function
Public Overrides Function GetHashCode() As Integer
Return precomputedHash
End Function
Public Shared Operator = (ByVal pt1 As Point, ByVal pt2 As Point) As Boolean
If pt1.
Pages:
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573