I have two objects that are something like this:
class Container {
public HashSet<Item> Items { get; }
}
class Item {
public Container Parent { get; set; }
public string Value1 { get; set; }
public int Value2 { get; set; }
}
Every Item
instance must belong to a Container
instance, and there is a one-to-many relationship between the two that is always kept in sync at both ends.
I am now faced with implementing a method that compares two instances of Item
to see if their Value1
and Value2
values match. The method will not take into consideration the Parent
value since each pair of instances I compare definitely differ on this value, so doing so would make the method I am implementing useless since it would then have the same result (false
) as the object.ReferenceEquals
method.
My question is as follows. Should I implement this method as the object's public override bool Equals( object obj )
method (along with GetHashCode
)? Or does the fact that it ignores its Parent
property preclude me from doing so? Why or why not? An alternative idea I have is to just implement it as public bool EqualsIgnoreParent( Item other )
which does not override anything; I could then call it from a custom comparer.