¿Cómo trato con campos nulos en la función GetHashCode
?GetHashCode en campos nulos?
Module Module1
Sub Main()
Dim c As New Contact
Dim hash = c.GetHashCode
End Sub
Public Class Contact : Implements IEquatable(Of Contact)
Public Name As String
Public Address As String
Public Overloads Function Equals(ByVal other As Contact) As Boolean _
Implements System.IEquatable(Of Contact).Equals
Return Name = other.Name AndAlso Address = other.Address
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If ReferenceEquals(Me, obj) Then Return True
If TypeOf obj Is Contact Then
Return Equals(DirectCast(obj, Contact))
Else
Return False
End If
End Function
Public Overrides Function GetHashCode() As Integer
Return Name.GetHashCode Xor Address.GetHashCode
End Function
End Class
End Module
XORing no es una buena manera de combinar códigos hash. Para un enfoque más robusto, consulte http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode –