Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


IStructuralEquatable Interface

Definition

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

public interface IStructuralEquatable
Derived

Examples

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 IEqualityComparer interface. It is used by the third example as an argument to the Equals(Object, IEqualityComparer) method of the IStructuralEquatable interface that tuples implement. 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.

using System;
using System.Collections;
using System.Collections.Generic;

public class NanComparer : IEqualityComparer
{
   public new bool Equals(object x, object y)
   {
      if (x is float)
         return (float) x == (float) y;
      else if (x is double)
         return (double) x == (double) y;
      else
         return EqualityComparer<object>.Default.Equals(x, y);
   }

   public int GetHashCode(object obj)
   {
      return EqualityComparer<object>.Default.GetHashCode(obj);
   }
}

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

public class Example
{
   public static void Main()
   {
      var t1 = Tuple.Create(12.3, Double.NaN, 16.4);
      var t2 = Tuple.Create(12.3, Double.NaN, 16.4);

      // Call default Equals method.
      Console.WriteLine(t1.Equals(t2));

      IStructuralEquatable equ = t1;
      // Call IStructuralEquatable.Equals using default comparer.
      Console.WriteLine(equ.Equals(t2, EqualityComparer<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()));
   }
}
// The example displays the following output:
//       True
//       True
//       True
//       False

Remarks

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.

Megjegyzés

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 default equality comparers, which are returned by the EqualityComparer<T>.Default and StructuralComparisons.StructuralEqualityComparer properties. For more information, see the example.

The generic tuple classes (Tuple<T1>, Tuple<T1,T2>, Tuple<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.

Methods

Equals(Object, IEqualityComparer)

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

GetHashCode(IEqualityComparer)

Returns a hash code for the current instance.

Applies to

Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also