I am new to C#. So please bear with me. I ran into a problem when I tried to compare two lists of different derived objects. I have a base class and a child class that inherits from it. The derived class has additional properties in the Equals() value comparison. I implement the IEquatable<> interface in the bass class as well as the derived class. I then fill two lists, each with one object from the derived class. The problem is when I call the Equals() function, it is the Base.Equals() that gets called. So basically the items in the list are treated as base class objects instead of the derived objects. How do I get out of this? See below code:
public class Base : IEquatable<Base>
{
...
public int BaseProperty {get; set;}
...
public virtual bool Equals(Base other)
{
...
return this.BaseProperty == other.BaseProperty;
}
}
public class Derived : Base, IEquatable<Derived>
{
public int DerivedProperty {get; set:}
...
}
public class myList : IList<Base>, IEquatable<myList>
{
private List<Base> _list;
...
public bool Equals(myList other)
{
...
return ENumerable.SequenceEqual(this._list.OrderBy(r => r), other._list.OrderBy(r = > r));
}
}
Main()
{
Derived first = new Derived() {...};
Derived second = new Derived() {...};
myList<Derived> list1 = new List<Derived>(){ first };
myList<Derived> list2 = new List<Derived>(){ second };
bool ret = list1.Equals(list2);
}
The comparison of the two lists is done using SequenceEqual. When I stepped into the calls, I could see the trace ended up in Base.Equals(Base other) instead of Derived.Equals(Derived other). I can't cast it because in reality I have multiple derived classes and the lists could potentially hold any numbers of any objects. So my question is is there a way to make C# respect the derived type(s) in this situation? Thanks.