Say you were writing the original C# Object
class and you wanted the following functionality:
object1 == object2
will compare references unless this operator is overriddenobject1 != object2
will always return the inverse of the object's ACTUALobject1 == object2
So, for example, if I have a Bunny
class (derived from Object
) which uses ear lengths as its equals method, then the notequals method (inherited from Object
) should return true if the bunnies have different ear lengths.
The problem that I see is that if I write my Object
class something like:
public partial class Object {
public virtual bool Equals(Object o2) {
return (this === o2);
}
public bool NotEquals(Object o2) {
return !this.Equals(o2);
}
}
then it seems like that definition will bind NotEquals to Object
's Equals, not the actual derived class's equals.
Is there some way that this can work without modifying C# itself in any way? I don't care so much that it's possible in C# but I care if there's some OOP principle which tells me that I shouldn't expect this sort of thing to work.
Also, I'm not sure whether or not this is fundamental to the question, but the idea is for NotEquals to be virtual as well so that it too can be overridden by derived classes which want their o1 != o2
to be different from !(o1 == o2)
. This question is inspired by this recent discussion.