gpt4 book ai didi

c# - 从 C# 3.0 中的匿名委托(delegate)返回

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:55 24 4
gpt4 key购买 nike

如果您在一个方法中并且传入了一个匿名委托(delegate),那么“return”关键字是返回匿名委托(delegate)的值,还是返回函数?我知道在 ruby​​ 中,他们使用“next”在 block 内实现这种类型的功能。

这是一个例子:

public bool X()
{
AList.Where(x =>
{
if (x.val == 1) return true;

....
return someBool;
}
...
return anotherBool
}

最佳答案

does the 'return' key word return a value for the anonymous delegate, or does it return the function?



它从匿名委托(delegate)返回。

.Where() 不返回 bool 值,您可以尝试 .Any()
public bool X()
{
bool result = AList.Any(x => x.val == 1);
return result;
}

或者您可以使用捕获的变量:
bool y = false;
Func<int, bool> checker = x =>
{
if (x == 1)
{
return true;
}
y = true;
return false;
}

AList.Where(checker).ToList();

关于c# - 从 C# 3.0 中的匿名委托(delegate)返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1463166/

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