IEquatable(Of T).Equals Method (T)
![]() |
---|
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience. |
Indicates whether the current object is equal to another object of the same type.
Assembly: mscorlib (in mscorlib.dll)
The implementation of the Equals method is intended to perform a test for equality with another object of type T, the same type as the current object. The Equals method is called in the following circumstances:
When the Equals method is called and the other parameter represents a strongly-typed object of type T. (If other is of type Object, the base Object.Equals(Object) method is called. Of the two methods, IEquatable(Of T).Equals offers slightly better performance.)
When the search methods of a number of generic collection objects are called. Some of these types and their methods include the following:
Some of the generic overloads of the BinarySearch method.
The search methods of the List(Of T) class, including List(Of T).Contains(T), List(Of T).IndexOf, List(Of T).LastIndexOf, and List(Of T).Remove.
The search methods of the Dictionary(Of TKey, TValue) class, including ContainsKey and Remove.
The search methods of the generic LinkedList(Of T) class, including LinkedList(Of T).Contains and Remove.
In other words, to handle the possibility that objects of a class will be stored in an array or a generic collection object, it is a good idea to implement IEquatable(Of T) so that the object can be easily identified and manipulated.
When implementing the Equals method, define equality appropriately for the type specified by the generic type argument. For example, if the type argument is Int32, define equality appropriately for the comparison of two 32-bit signed integers.
Notes to Implementers:
If you implement Equals, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable(Of T).Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results, which the example illustrates.
The following example shows the partial implementation of a Person class that implements IEquatable(Of T) and has two properties, LastName and SSN. The Equals method returns True if the SSN property of two Person objects is identical; otherwise, it returns False.
Imports System.Collections.Generic Imports System.Text.RegularExpressions Public Class Person : Implements IEquatable(Of Person) Private uniqueSsn As String Private lName As String Public Sub New(lastName As String, ssn As String) If Regex.IsMatch(ssn, "\d{9}") Then uniqueSsn = String.Format("{0}-(1}-{2}", ssn.Substring(0, 3), _ ssn.Substring(3, 2), _ ssn.Substring(5, 4)) ElseIf Regex.IsMatch(ssn, "\d{3}-\d{2}-\d{4}") Then uniqueSsn = ssn Else Throw New FormatException("The social security number has an invalid format.") End If Me.uniqueSsn = ssn Me.LastName = lastName End Sub Public ReadOnly Property SSN As String Get Return Me.uniqueSsn End Get End Property Public Property LastName As String Get Return Me.lName End Get Set If String.IsNullOrEmpty(value) Then Throw New ArgumentException("The last name cannot be null or empty.") Else lname = value End If End Set End Property Public Overloads Function Equals(other As Person) As Boolean _ Implements IEquatable(Of Person).Equals If other Is Nothing Then Return False If Me.uniqueSsn = other.uniqueSsn Then Return True Else Return False End If End Function Public Overrides Function Equals(obj As Object) As Boolean If obj Is Nothing Then Return False Dim personObj As Person = TryCast(obj, Person) If personObj Is Nothing Then Return False Else Return Equals(personObj) End If End Function Public Overrides Function GetHashCode() As Integer Return Me.SSN.GetHashCode() End Function Public Shared Operator = (person1 As Person, person2 As Person) As Boolean If person1 Is Nothing OrElse person2 Is Nothing Then Return Object.Equals(person1, person2) End If Return person1.Equals(person2) End Operator Public Shared Operator <> (person1 As Person, person2 As Person) As Boolean If person1 Is Nothing OrElse person2 Is Nothing Then Return Not Object.Equals(person1, person2) End If Return Not person1.Equals(person2) End Operator End Class
Person objects can then be stored in a List(Of T) object and can be identified by the Contains method, as the following example shows.
Module TestIEquatable Public Sub Main() ' Create a Person object for each job applicant. Dim applicant1 As New Person("Jones", "099-29-4999") Dim applicant2 As New Person("Jones", "199-29-3999") Dim applicant3 As New Person("Jones", "299-49-6999") ' Add applicants to a List object. Dim applicants As New List(Of Person) applicants.Add(applicant1) applicants.Add(applicant2) applicants.Add(applicant3) ' Create a Person object for the final candidate. Dim candidate As New Person("Jones", "199-29-3999") If applicants.Contains(candidate) Then Console.WriteLine("Found {0} (SSN {1}).", _ candidate.LastName, candidate.SSN) Else Console.WriteLine("Applicant {0} not found.", candidate.SSN) End If ' Call the shared inherited Equals(Object, Object) method. ' It will in turn call the IEquatable(Of T).Equals implementation. Console.WriteLine("{0}({1}) already on file: {2}.", _ applicant2.LastName, _ applicant2.SSN, _ Person.Equals(applicant2, candidate)) End Sub End Module ' The example displays the following output: ' Found Jones (SSN 199-29-3999). ' Jones(199-29-3999) already on file: True.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1