Defines methods to support the comparison of objects for structural equality.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
Structural equality means that two objects are equal because they have equal values. It differs from reference equality, which indicates that two object references are equal because they reference the same physical object. The interface enables you to implement customized comparisons to check for the structural equality of collection objects. That is, you can create your own definition of structural equality and specify that this definition be used with a collection type that accepts the interface. The interface has two members: Equals, which tests for equality by using a specified IEqualityComparer implementation, and GetHashCode, which returns identical hash codes for objects that are equal.
The interface supports only custom comparisons for structural equality. The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.
The .NET Framework also provides default equality comparers, which are returned by the EqualityComparer(Of T).Default and StructuralComparisons.StructuralEqualityComparer properties. For more information, see the example.
The generic tuple classes (Tuple(Of T1), Tuple(Of T1, T2), Tuple(Of T1, T2, T3), and so on) and the Array class provide explicit implementations of the interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an interface value and providing your IEqualityComparer implementation as an argument to the Equals method, you can define a custom equality comparison for the array or collection.
The default equality comparer, EqualityComparer<Object>.Default.Equals, considers two NaN values to be equal. However, in some cases, you may want the comparison of NaN values for equality to return false, which indicates that the values cannot be compared. The following example defines a NanComparer class that implements the interface. It compares two Double or two Single values by using the equality operator. It passes values of any other type to the default equality comparer.
Imports System.Collections Imports System.Collections.Generic Public Class NanComparer : Implements IEqualityComparer Public Overloads Function Equals(x As Object, y As Object) As Boolean _ Implements IEqualityComparer.Equals If TypeOf x Is Single Then Return CSng(x) = CSng(y) ElseIf TypeOf x Is Double Then Return CDbl(x) = CDbl(y) Else Return EqualityComparer(Of Object).Default.Equals(x, y) End If End Function Public Overloads Function GetHashCode(obj As Object) As Integer _ Implements IEqualityComparer.GetHashCode Return EqualityComparer(Of Object).Default.GetHashCode(obj) End Function End Class
The following example creates two identical 3-tuple objects whose components consist of three Double values. The value of the second component is Double.NaN. The example then calls the Tuple(Of T1, T2, T3).Equals method, and it calls the IStructuralEquatable.Equals method three times. The first time, it passes the default equality comparer that is returned by the EqualityComparer(Of T).Default property. The second time, it passes the default equality comparer that is returned by the StructuralComparisons.StructuralEqualityComparer property. The third time, it passes the custom NanComparer object. As the output from the example shows, the first three method calls return true, whereas the fourth call returns false.
Module Example Public Sub Main() Dim t1 = Tuple.Create(12.3, Double.NaN, 16.4) Dim t2 = Tuple.Create(12.3, Double.NaN, 16.4) ' Call default Equals method. Console.WriteLine(t1.Equals(t2)) Dim equ As IStructuralEquatable = t1 ' Call IStructuralEquatable.Equals using default comparer. Console.WriteLine(equ.Equals(t2, EqualityComparer(Of Object).Default)) ' Call IStructuralEquatable.Equals using ' StructuralComparisons.StructuralEqualityComparer. Console.WriteLine(equ.Equals(t2, StructuralComparisons.StructuralEqualityComparer)) ' Call IStructuralEquatable.Equals using custom comparer. Console.WriteLine(equ.Equals(t2, New NanComparer)) End Sub End Module ' The example displays the following output: ' True ' True ' True ' False
.NET FrameworkSupported in: 4.5, 4
.NET Framework Client ProfileSupported in: 4
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.