Search code examples
c#.neticomparer

Quick IComparer?


Before I go reinventing the wheel, is there some framework way of creating an IComparer<T> from a Func<T,T,int>?

EDIT

IIRC (it's been a while) Java supports anonymous interface implementations. Does such a construct exist in C#, or are delegates considered a complete alternative?


Solution

  • In the upcoming .NET4.5 (Visual Studio 2012) this is possible with the static factory method Comparer<>.Create. For example

    IComparer<Person> comp = Comparer<Person>.Create(
        (p1, p2) => p1.Age.CompareTo(p2.Age)
        );