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
)
Even though you can't create anonymous classes that implement interfaces, you can usually use the Comparison Delegate instead of the IComparer Interface in most cases (like sorting, etc.):
Also there are some built-in implementations of
IComparer
like the Comparer Class or the StringComparer Class...No, this is not possible. However, you can get the default implementation of
IComparer<TKey>
byComparer<TKey>.Default
. Otherwise you'll need to create a parameterized implementation and use an instance of that.