Re: Объясните насчет переопределения Equals(object)
От: StatujaLeha на правах ИМХО
Дата: 17.05.18 19:10
Оценка: 6 (1)
Здравствуйте, Коваленко Дмитрий, Вы писали:


КД>По-моему по умолчанию он делает ровно тоже самое. Не?


Неа.
https://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object.


Попробуй вот этот кода запустить.
    class MyClass: IEquatable<MyClass>
    {
        public int a = 1;

        public bool Equals(MyClass other)
        {
            Console.WriteLine("Typed Equals is called");
            return this.a.Equals(other.a);
        }
    }

    public partial class Program
    {
        public static void Main(string[] args)
        {
            object a = new MyClass();
            object b = new MyClass();

            Console.WriteLine(a.Equals(b)); // Типизированный Equals не вызовется.
            Console.WriteLine(((MyClass)a).Equals((MyClass)b));
        }
    }


Вывод:

False
Typed Equals is called
True

 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.