Equals Method
Collapse the table of content
Expand the table of content

IStructuralEquatable.Equals Method (Object, IEqualityComparer)


Determines whether an object is structurally equal to the current instance.

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

Function Equals (
	other As Object,
	comparer As IEqualityComparer
) As Boolean


Type: System.Object

The object to compare with the current instance.

Type: System.Collections.IEqualityComparer

An object that determines whether the current instance and other are equal.

Return Value

Type: System.Boolean

true if the two objects are equal; otherwise, false.

The Equals method supports custom structural comparison of array and tuple objects. This method in turn calls the comparer object's IEqualityComparer.Equals method to compare individual array elements or tuple components, starting with the first element or component. The individual calls to IEqualityComparer.Equals end and the IStructuralEquatable.Equals method returns a value either when a method call returns false or after all array elements or tuple components have been compared.

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 IStructuralEquatable 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.

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

    Public Shared Sub Main()

        Dim doc As New XmlDocument()
        doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _

        Dim root As XmlNode = doc.FirstChild

        'Create a new node.
        Dim prefix As String = root.GetPrefixOfNamespace("urn:samples")
        Dim elem As XmlElement = doc.CreateElement(prefix, "style", "urn:samples")
        elem.InnerText = "hardcover"

        'Add the node to the document.

        Console.WriteLine("Display the modified XML...")
    End Sub 'Main 
End Class 'Sample

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.

      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.

      ' 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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft