匿名IComparer实现 [英] Anonymous IComparer implementation
本文介绍了匿名IComparer实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能定义一个匿名实现的IComparer的
我相信Java允许匿名类中定义的内联 - ?不C#
看着这个代码,我想自定义一个的IComparer
在线
公共静态IOrderedEnumerable< TSource>排序依据< TSource,TKEY的>(
本的IEnumerable< TSource>源,
Func键< TSource,TKEY的>的KeySelectors,
的IComparer< TKEY的>比较器
)
解决方案
没有,C#目前不允许内嵌接口实现;虽然它的确实的允许您通过lambda表达式和匿名方法创建的代表的内联。
在你的情况,我建议使用 ProjectionComparer
,可以很容易使用这个功能,比如上市的here 。
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 ProjectionComparer
that makes it easy to use this feature, such as the one listed here.
这篇关于匿名IComparer实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文