c# – 关于列表排序和委托&lambda表达式Func的东西

前端之家收集整理的这篇文章主要介绍了c# – 关于列表排序和委托&lambda表达式Func的东西前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_2@List<bool> test = new List<bool>(); test.Sort(new Func<bool,bool,int>((b1,b2) => 1));

我错过了什么?

Error 2 Argument 1: cannot convert from ‘System.Func’ to ‘System.Collections.Generic.IComparer’

Error 1 The best overloaded method match for ‘System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)’ has some invalid arguments

当我有

@H_403_2@private int func(bool b1,bool b2) { return 1; } private void something() { List<bool> test = new List<bool>(); test.Sort(func); }

它工作正常.他们不是一回事吗?

解决方法

Func是错误的委托类型.您可以使用以下任一方法: @H_403_2@test.Sort((b1,b2) => 1); test.Sort(new Comparison<bool>((b1,b2) => 1));

猜你在找的C#相关文章