gpt4 book ai didi

c# - 关于列表排序和委托(delegate)和 lambda 表达式 Func 的东西

转载 作者:太空狗 更新时间:2023-10-29 21:09:47 24 4
gpt4 key购买 nike

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 是错误的委托(delegate)类型。您可以使用以下任一方法:

test.Sort((b1, b2) => 1);
test.Sort(new Comparison<bool>((b1, b2) => 1));

关于c# - 关于列表排序和委托(delegate)和 lambda 表达式 Func 的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com