An IEqualityComparer
is an interface that allows to create a custom euqality comparer. You can use it for example in many LINQ methods like Intersect
,Except
,GroupBy
,Join
etc. But you have to pass an instance of the comparer to these methods.
If you want to change override Equals
and GetHashCode
in general:
Public Class Person
Implements IDatabaseEntity
Public Property Id As Integer Implements IDatabaseEntity.Id
Public Overrides Function Equals(obj As Object) As Boolean
If obj Is Nothing Then Return False
If Object.ReferenceEquals(Me, obj) Then Return True
Dim Person2 As Person = TryCast(obj, Person)
If Person2 Is Nothing Then Return False
Return Person2.Id = Id
End Function
Public Overrides Function GetHashCode() As Integer
Return Id
End Function
End Class
If you want to use your comparer, you can use it's Equals
in this way:
Dim comparer = New DatabaseEntityEqualityComparer()
If comparer.Equals(p1, p2) Then
Response.Write("Equals")
End If