|
|
От: |
_FRED_
|
@ViIvanov |
| Дата: | 18.10.23 20:02 | ||
| Оценка: | 53 (1) | ||
(отсюда)The EqualityContract enables the equality methods to compare the runtime type of objects when they're checking for equality.
using System;
var r = new R("A");
var x = new X("A");
Console.WriteLine($"Compare {nameof(r)} and {nameof(x)}: {r == x}"); // False
var r1 = new R1("A");
var x1 = new X1("A");
Console.WriteLine($"Compare {nameof(r1)} and {nameof(x1)}: {r1 == x1}"); // True
record R(string Name) { }
record X(string Name) : R(Name) { }
record R1(string Name) {
public virtual bool Equals(R1? other) => other is not null && other.Name == Name;
public override int GetHashCode() => Name?.GetHashCode() ?? 0;
}
record X1(string Name) : R1(Name) { }