gpt4 book ai didi

c# - 用 lambda 表达式替换委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 13:09:28 25 4
gpt4 key购买 nike

我正在尝试用 lambda 表达式替换以下语句:

 List<ABC> l = new List<ABC>();
l.Find(delegate(ABC a) { return a.A == 4; });

我试过了

 l.Find((ABC a)=>a.A==4;);

但这显然是不正确的。谢谢

最佳答案

为了完整起见,以下任何一个都是有效的:

// Fullest version
l.Find((ABC a) => { return a.A==4; });

// Infer the type of the parameter
l.Find((a) => { return a.A==4; });

// Single parameter - can remove the ()
l.Find(a => { return a.A==4; });

// Single expression - can remove braces and semi-colon
l.Find(a => a.A == 4);

(您可以独立于其他快捷方式使用“单一表达式”部分。)

关于c# - 用 lambda 表达式替换委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9563455/

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