Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not use IComparable<T> on ancestor class and compare child classes?

I'm trying to sort a list of objects using List.Sort(), but at runtime it tells me that it cannot compare elements in the array.

Failed to compare two elements in the array

Class structure:

public abstract class Parent : IComparable<Parent> {
    public string Title;
    public Parent(string title){this.Title = title;}

    public int CompareTo(Parent other){
        return this.Title.CompareTo(other.Title);
    }
}

public class Child : Parent {
    public Child(string title):base(title){}
}

List<Child> children = GetChildren();
children.Sort(); //Fails with "Failed to compare two elements in the array."

Why can I not compare subclasses of a base that implements IComparable<T>? I'm probably missing something, but I cannot see why this should not be allowed.

Edit: Should clarify that I'm targeting .NET 3.5 (SharePoint 2010)

Edit2: .NET 3.5 is the problem (see answer below).

like image 990
Thomas Avatar asked May 15 '13 11:05

Thomas


People also ask

When to use IComparable in c#?

Use the IComparable Interface in C# to sort elements. It is also used to compare the current instance with another object of same type. It provides you with a method of comparing two objects of a particular type. Remember, while implementing the IComparable interface, CompareTo() method should also be implemented.

What is IComparer in c#?

C# IComparer interface The IComparer interface defines a comparison method that a value type or class implements to order or sort its instances. This interface is used with the List. Sort and List. BinarySearch methods. It provides a way to customize the sort order of a collection.


1 Answers

I assume this is a .NET version before .NET 4.0; after .NET 4.0 it is IComparable<in T>, and should work OK in many cases - but this requires the variance changes in 4.0

The list is List<Child> - so sorting it will try to use either IComparable<Child> or IComparable - but neither of those is implemented. You could implement IComparable at the Parent level, perhaps:

public abstract class Parent : IComparable<Parent>, IComparable {
    public string Title;
    public Parent(string title){this.Title = title;}

    int IComparable.CompareTo(object other) {
        return CompareTo((Parent)other);
    }
    public int CompareTo(Parent other){
        return this.Title.CompareTo(other.Title);
    }
}

which will apply the same logic via object.

like image 136
Marc Gravell Avatar answered Sep 27 '22 17:09

Marc Gravell