Consider:
class Base : IEquatable<Base> {
public bool Equals(Base other) => false;
public override bool Equals(Object o) => false;
public override int GetHashCode() => 0;
public static bool operator ==(Base o1, Base o2) => Equals(o1, o2);
public static bool operator !=(Base o1, Base o2) => !Equals(o1, o2);
}
class Derived : Base {
public static bool operator ==(Derived o1, Derived o2) => Equals(o1, o2);
public static bool operator !=(Derived o1, Derived o2) => !Equals(o1, o2);
}
I get:
Warning CS0660 'Derived' defines operator == or operator != but does not override Object.Equals(object o)
Warning CS0661 'Derived' defines operator == or operator != but does not override Object.GetHashCode()
These warnings are to me wrong - as I inherit overridden Equals(object)
and GetHashCode()
from Base the code should not have any warnings.
Moreover there is no great way out of it -
I can implement the functions in Derived which is ugly and superfluous
I can add #pragma warning disable CS0660, CS0661
but that will disable the warnings everywhere in the file, not only where the functions are safely derived.
Is this an oversight of the C# compiler or is there a good reason for it ? Is there a clean way out of it ?