IEqualityComparer.GetHashCode Method (Object)


Returns a hash code for the specified object.

Namespace:   System.Collections
Assembly:  mscorlib (in mscorlib.dll)

Function GetHashCode (
	obj As Object
) As Integer


Type: System.Object

The Object for which a hash code is to be returned.

Return Value

Type: System.Int32

A hash code for the specified object.

Exception Condition

The type of obj is a reference type and obj is null.

Implement this method to provide customized hash codes for objects,corresponding to the customized equality comparison provided by the Equals method.

Notes to Implementers:

Implementations are required to ensure that if the Equals method returns true for two objects x and y, then the value returned by the GetHashCode method for x must equal the value returned for y.

The following code example demonstrates the implementation of a case-insensitive IEqualityComparer. In this example, the GetHashCode method returns the hash code provided by the Object type.

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top