Lezen in het Engels Bewerken

Delen via


IEquatable<T> Interface

Definition

Defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances.

public interface IEquatable<T>

Type Parameters

T

The type of objects to compare.

Derived

Examples

The following example shows the partial implementation of a Person class that implements IEquatable<T> and has two properties, LastName and NationalId. NationalId is considered to be a unique identifier, therefore the Equals method returns True if the NationalId property of two Person objects is identical; otherwise, it returns False.
(Note that the F# example does not handle null values for Person instances.)

public class Person : IEquatable<Person>
{
    public Person(string lastName, string ssn)
    {
        LastName = lastName;
        NationalId = ssn;
    }

    public string LastName { get; }

    public string NationalId { get; }

    public bool Equals(Person? other) => other is not null && other.NationalId == NationalId;

    public override bool Equals(object? obj) => Equals(obj as Person);

    public override int GetHashCode() => NationalId.GetHashCode();

    public static bool operator ==(Person person1, Person person2)
    {
        if (person1 is null)
        {
            return person2 is null;
        }

        return person1.Equals(person2);
    }

    public static bool operator !=(Person person1, Person person2)
    {
        if (person1 is null)
        {
            return person2 is not null;
        }

        return !person1.Equals(person2);
    }
}

Remarks

This interface is implemented by types whose values can be equated (for example, the numeric and string classes). A value type or class implements the Equals method to create a type-specific method suitable for determining equality of instances.

Notitie

The IComparable<T> interface defines the CompareTo method, which determines the sort order of instances of the implementing type. The IEquatable<T> interface defines the Equals method, which determines the equality of instances of the implementing type.

The IEquatable<T> interface is used by generic collection objects such as Dictionary<TKey,TValue>, List<T>, and LinkedList<T> when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.

For more information about implementing the IEquatable<T> interface, see remarks on the IEquatable<T>.Equals method.

Notes to Implementers

Replace the type parameter of the IEquatable<T> interface with the type that is implementing this interface.

If you implement IEquatable<T>, you should also override the base class implementations of Equals(Object) and GetHashCode() so that their behavior is consistent with that of the Equals(T) method. If you do override Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results.

For information on overriding Equals(Object), see Equals(Object).

For a value type, you should always implement IEquatable<T> and override Equals(Object) for better performance. Equals(Object) boxes value types and relies on reflection to compare two values for equality. Both your implementation of Equals(T) and your override of Equals(Object) should return consistent results.

If you implement IEquatable<T>, you should also implement IComparable<T> if instances of your type can be ordered or sorted. If your type implements IComparable<T>, you almost always also implement IEquatable<T>.

Note that there are some designs where a type supports an order relation, but equality may be distinct from an ordering relation. Consider a Person class where you sort alphabetically. Two people with the same name sort the same, but are not the same person.

Methods

Equals(T)

Indicates whether the current object is equal to another object of the same type.

Applies to

Product Versies
.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, 10
.NET Framework 2.0, 3.0, 3.5, 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