IStructuralEquatable Arabirim
Tanım
Yapısal eşitlik için nesnelerin karşılaştırmasını destekleme yöntemlerini tanımlar.Defines methods to support the comparison of objects for structural equality.
public interface class IStructuralEquatable
public interface IStructuralEquatable
type IStructuralEquatable = interface
Public Interface IStructuralEquatable
- Türetilmiş
Örnekler
Varsayılan eşitlik karşılaştırıcısı, EqualityComparer<Object>.Default.Equals
iki NaN
değeri eşit olacak şekilde değerlendirir.The default equality comparer, EqualityComparer<Object>.Default.Equals
, considers two NaN
values to be equal. Ancak bazı durumlarda, NaN
eşitlik için değerlerin karşılaştırılmasını false
, değerlerin karşılaştırılamayacağını gösteren şekilde karşılaştırmayı isteyebilirsiniz.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. Aşağıdaki örnek, NanComparer
arabirimini uygulayan bir sınıfı tanımlar IEqualityComparer .The following example defines a NanComparer
class that implements the IEqualityComparer interface. Bu, üçüncü örnek tarafından Equals(Object, IEqualityComparer) başlıkların uygulayan arabirim yöntemine bir bağımsız değişken olarak kullanılır IStructuralEquatable .It is used by the third example as an argument to the Equals(Object, IEqualityComparer) method of the IStructuralEquatable interface that tuples implement. Double Single Eşitlik işlecini kullanarak iki veya iki değeri karşılaştırır.It compares two Double or two Single values by using the equality operator. Diğer tüm tür değerlerini varsayılan eşitlik karşılaştırıcısı 'na geçirir.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);
}
}
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
Aşağıdaki örnek, bileşenleri üç değerden oluşan iki özdeş 3 demet nesne oluşturur Double .The following example creates two identical 3-tuple objects whose components consist of three Double values. İkinci bileşenin değeri Double.NaN .The value of the second component is Double.NaN. Örnek daha sonra yöntemini çağırır Tuple<T1,T2,T3>.Equals ve IStructuralEquatable.Equals yöntemi üç kez çağırır.The example then calls the Tuple<T1,T2,T3>.Equals method, and it calls the IStructuralEquatable.Equals method three times. İlk kez, özelliği tarafından döndürülen varsayılan eşitlik karşılaştırıcıyı geçirir EqualityComparer<T>.Default .The first time, it passes the default equality comparer that is returned by the EqualityComparer<T>.Default property. İkinci kez, özelliği tarafından döndürülen varsayılan eşitlik karşılaştırıcıyı geçirir StructuralComparisons.StructuralEqualityComparer .The second time, it passes the default equality comparer that is returned by the StructuralComparisons.StructuralEqualityComparer property. Üçüncü kez özel NanComparer
nesneyi geçirir.The third time, it passes the custom NanComparer
object. Örnekteki Çıktının gösterdiği gibi, ilk üç yöntem döner true
, ancak dördüncü çağrı döndürülür false
.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
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
Açıklamalar
Yapısal eşitlik, eşit değerlere sahip olduklarından iki nesnenin eşit olduğu anlamına gelir.Structural equality means that two objects are equal because they have equal values. Aynı fiziksel nesneye başvurduğundan, iki nesne başvurusunun eşit olduğunu gösteren başvuru eşitliğine göre farklılık gösterir.It differs from reference equality, which indicates that two object references are equal because they reference the same physical object. IStructuralEquatableArabirim, koleksiyon nesnelerinin yapısal eşitliğini denetlemek için özelleştirilmiş karşılaştırmalar uygulamanıza olanak sağlar.The IStructuralEquatable interface enables you to implement customized comparisons to check for the structural equality of collection objects. Diğer bir deyişle, kendi yapısal eşitlik tanımınızı oluşturabilir ve bu tanımın arabirimi kabul eden bir koleksiyon türüyle kullanılacağını belirtebilirsiniz IStructuralEquatable .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. Arabirim iki üyeye sahiptir: Equals , belirtilen uygulamayı kullanarak eşitlik için test eder IEqualityComparer ve GetHashCode eşit olan nesneler için özdeş karma kodları döndürür.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.
Not
IStructuralEquatableArabirim, yapısal eşitlik için yalnızca özel karşılaştırmaları destekler.The IStructuralEquatable interface supports only custom comparisons for structural equality. IStructuralComparableArabirim sıralama ve sıralama için özel yapısal karşılaştırmaları destekler.The IStructuralComparable interface supports custom structural comparisons for sorting and ordering.
.NET Framework Ayrıca, ve özellikleri tarafından döndürülen varsayılan eşitlik Karşılaştırıcılar sağlar EqualityComparer<T>.Default StructuralComparisons.StructuralEqualityComparer .The .NET Framework also provides default equality comparers, which are returned by the EqualityComparer<T>.Default and StructuralComparisons.StructuralEqualityComparer properties. Daha fazla bilgi için örneğe bakın.For more information, see the example.
Genel demet sınıfları ( Tuple<T1> , Tuple<T1,T2> , vb Tuple<T1,T2,T3> .) ve Array sınıfı, arabirimin açık uygulamalarını sağlar IStructuralEquatable .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. Atama (C# ' de) veya bir dizi ya da tanımlama alanının geçerli örneğini bir arabirim değerine dönüştürerek (Visual Basic içinde) ya da IStructuralEquatable IEqualityComparer uygulamanızı yöntemine bağımsız değişken olarak sağlayarak Equals , dizi veya koleksiyon için özel bir eşitlik karşılaştırması tanımlayabilirsiniz.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.
Yöntemler
Equals(Object, IEqualityComparer) |
Bir nesnenin yapısal olarak geçerli örneğe eşit olup olmadığını belirler.Determines whether an object is structurally equal to the current instance. |
GetHashCode(IEqualityComparer) |
Geçerli örnek için bir karma kod döndürür.Returns a hash code for the current instance. |