Export (0) Print
Expand All

IStructuralEquatable Interface

Defines methods to support the comparison of objects for structural equality.

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

'Declaration
Public Interface IStructuralEquatable

The IStructuralEquatable type exposes the following members.

  NameDescription
Public methodEqualsDetermines whether an object is structurally equal to the current instance.
Public methodGetHashCodeReturns a hash code for the current instance.
Top

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

NoteNote:

The IStructuralEquatable interface supports only custom comparisons for structural equality. The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.

The .NET Framework also provides a default equality comparer, which is returned by the EqualityComparer(Of T).Default property. 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 IStructuralEquatable interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralEquatable 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. In some cases, however, 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.

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 twice. 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 custom NanComparer object. As the output from the example shows, the first two method calls return true, whereas the third call returns false.

Silverlight

Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft