GetHashCode on null fields?

Posted by Shimmy on Stack Overflow See other posts from Stack Overflow or by Shimmy
Published on 2010-03-15T02:27:41Z Indexed on 2010/03/15 2:29 UTC
Read the original article Hit count: 402

How do I deal with null fields in GetHashCode function?

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

© Stack Overflow or respective owner

Related posts about iequatable

Related posts about gethashcode