Is there any way to implement specialized IComparer for the base class type so a child class could still use it for sorting in speciliazed collections?
Example
public class A
{
public int X;
}
public class B:A
{
public int Y;
}
public AComparer:IComparer<A>
{
int Compare(A p1, A p2)
{
//...
}
}
so following code will work:
List<A> aList = new List<A>();
aList.Sort(new AComparer());
List<B> bList = new List<B>();
bList.Sort(new AComparer()); // <- this line fails due to type cast issues
How to approach this issue to have both – inheritance of sorting and specialized collections (and do not copy IComparer classes for each of children classes?
Thanks in advance!
Firstly, note that this is fixed in .NET 4 via generic contravariance – your code would simply work. EDIT: As noted in comments, generic variance was first supported in CLR v2, but various interfaces and delegates only became covariant or contravariant in .NET 4.
However, in .NET 2 it’s still fairly easy to create a converter:
You can then use:
EDIT: There’s nothing you can do without changing the way of calling it at all. You could potentially make your
AComparergeneric though:Then you could use:
Of course, this means making all your comparer implementations generic, and it’s somewhat ugly IMO.