Why implement the IEquatable <T> interface

I read articles and understood interfaces to such an extent, although if I wanted to change my own Equals method, it seems I can do this without implementing IEquatable Interface. Example.

using System;
using System.Collections;
using System.ComponentModel;

namespace ProviderJSONConverter.Data.Components
{
    public class Address : IEquatable<Address>
    {
        public string address { get; set; }

        [DefaultValue("")]
        public string address_2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }

        public bool Equals(Address other)
        {
            if (Object.ReferenceEquals(other, null)) return false;
            if (Object.ReferenceEquals(this, other)) return true;
            return (this.address.Equals(other.address)
                && this.address_2.Equals(other.address_2)
                && this.city.Equals(other.city)
                && this.state.Equals(other.state)
                && this.zip.Equals(other.zip));
        }
    }
 }

Now, if I do not implement the interface and leave it : IEquatable<Address>out of code, it seems that the application works the same way. Therefore, I do not understand why to implement the interface? I can write my own Equals method without it, and a breakpoint will delete the method still and return the same results. Can anyone help explain this to me more? I hung up on why to include " IEquatable<Address>" before calling the Equals method.

+2
4

, : IEquatable , , .

, , . :

List<Address> addresses = new List<Address>
{
    new Address { ... }
};
int index = addresses.IndexOf(new Address { ... });

... (.. index -1), Equals(object), IEquatable<T>. List<T>.IndexOf Equals.

, , Equals, (, , LINQ to Objects ..), , .

+8

Equals(object obj), , : IEquatable Object.Equals()?

, Equals(object obj), Equals(Adddress obj), IEquatable<T>, , , , Equals, .

, , EqualityComparer<Address>.Default, List<Address>.IndexOf , , Equals.

+2

IEquatable Equals , . funciton .

IEquatable Employee, Employee . Equals , Object param, , Object to struct . , IEquatable <Employee> .

, , Employee

if(e1.Equals(e2))
{
   //do some
}

Equals with Employee . , .

 struct Employee : IEquatable<Employee>
{
    public int Id { get; set; }

    public bool Equals(Employee other)
    {
        //no boxing not unboxing, direct compare
        return this.Id == other.Id;
    }

    public override bool Equals(object obj)
    {
        if(obj is Employee)
        {   //un boxing
            return ((Employee)obj).Id==this.Id;
        }
        return base.Equals(obj);
    }
}

:

Int IEquatable <int>

Bool IEquatable <bool>

IEquatable <float>

, someInt.Equals(1), Equals (object). Equals (int).

+1

.NET :

  • Object.Equals(object)
  • (==, !=, <=, >=)
  • IEquatable<T>.Equals(T)
  • IComparable.CompareTo(object)
  • IComparable<T>.CompareTo(T)
  • IEqualityComparer.Equals(object, object)
  • IEqualityComparer<T>.Equals(T, T)
  • IComparer.Compare(object, object)
  • IComparer<T>.Compare(T, T)

ReferenceEquals, Object.Equals(object, object) (, ), , - .

, . , , .

, :

, .

  • Equals(object) (==, !=) .
  • , Equals ( GetHashCode; -)
  • == !=, . , Equals.
  • , IComparable. Equals , CompareTo 0 ( , ).

, . IEquatable<T> Comparable<T> : , . , , .

  • Equals () ( ). , , . Equals(object) , , .
  • Equals(object) , , ( , ValueType.Equals, ). IEquatable<T>. , . , ?
  • == != , , . IEquatable<T>.Equals(T).
  • , " ", IComparable. IComparable<T>, (, Array.Sort, List<T>.BinarySearch, SortedList<TKey, TValue> ..). ==, !=, <, >, <=, >= .

: , 6. 9. . ( , self Equals ), , - .

+1
source

Source: https://habr.com/ru/post/1617619/


All Articles