Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SortedSet<T> and anonymous IComparer<T> in the constructor is not working

How come anonymous functions works as arguments on methods, but not in constructor arguments?


If I create a List<string>, it has a Sort method with the following signature:

public void Sort(IComparer<T> comparer)

where the following works:

List<string> list = new List<string>();
list.Sort( (a,b) => a.CompareTo(b) );

SortedSet has a constructor with a similar signature:

public SortedSet(IComparer<T> comparer)

but this fails when using an anonymous function in the constructor. The following is not valid:

SortedSet<string> set = new SortedSet<string>( (a, b) => a.CompareTo(b) );

Creating a sorting class works fine as expected:

public class MyComparer : IComparer<string>
{
    public int Compare(string a, string b)
    { return a.CompareTo(b); }
}

SortedSet<string> set = new SortedSet<string>( new MyComparer() );
like image 714
Mikael Svenson Avatar asked Jun 28 '10 08:06

Mikael Svenson


2 Answers

The .NET Framework 4.5 release introduced the factory method Comparer<T>.Create:

var set = new SortedSet<string>(
    Comparer<string>.Create((a, b) => a.CompareTo(b)));
like image 63
Phil Klein Avatar answered Oct 09 '22 13:10

Phil Klein


That it is because the constructor accepts an IComparer<T> (interface), rather than a Comparison<T> (delegate). Anon-methods / lambdas can support delegates, but not (directly) interfaces. It is pretty easy to wrap, though - for example:

class FuncComparer<T> : IComparer<T>
{
    private readonly Comparison<T> comparison;
    public FuncComparer(Comparison<T> comparison) {
        this.comparison = comparison;
    }
    public int Compare(T x, T y) {
        return comparison(x, y); }
}

Now you can use:

SortedSet<string> set = new SortedSet<string>(
      new FuncComparer<string>( (a, b) => a.CompareTo(b) ));
like image 26
Marc Gravell Avatar answered Oct 09 '22 14:10

Marc Gravell