Is it possible to define an anonymous implementation of IComparer?
I believe Java allows anonymous classes to be defined inline – does C#?
Looking at this code I want to define a custom IComparer inline
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer
)
No, C# does not currently allow inline interface implementations; although it does allow you to create delegates inline through lambda expressions and anonymous methods.
In your case, I would suggest using a
ProjectionComparerthat makes it easy to use this feature, such as the one listed here.