От: | StatujaLeha | на правах ИМХО | |
Дата: | 17.05.18 19:10 | ||
Оценка: | 6 (1) |
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