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

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

概述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
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

当我有

private int func(bool b1,bool b2){    return 1;}private voID something(){    List<bool> test = new List<bool>();    test.sort(func);}

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

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

以上是内存溢出为你收集整理的c# – 关于列表排序和委托&lambda表达式Func的东西全部内容,希望文章能够帮你解决c# – 关于列表排序和委托&lambda表达式Func的东西所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1262680.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存