Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

后端 未结 8 1956
闹比i
闹比i 2020-12-08 08:57

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityCom

8条回答
  •  眼角桃花
    2020-12-08 09:34

    These methods don't have overloads that accept a delegate instead of an interface, but:

    • You can normally return a simpler sort key through the delegate you pass to Enumerable.OrderBy
    • Likewise, you could call Enumerable.Select before calling Enumerable.SequenceEqual
    • It should be straightforward to write a wrapper that implements IEqualityComparer in terms of Func
    • F# lets you implement this sort of interface in terms of a lambda :)

提交回复
热议问题