ForEach lambda表达式是否具有可选的返回类型

Mar*_*cks 1 .net c# lambda

有没有办法让ForEachlambda表达式具有可选的返回类型.这是一个需要实现的伪代码示例:

string val = MyList.ForEach(listItem => { if(listItem == "yes" ){ return "found" }  });

if(val == "found"){ dosomething }
Run Code Online (Sandbox Code Playgroud)

nvo*_*igt 6

不,ForEach是你的结果的错误方法.用途Any:

bool found = MyList.Any(listItem => listItem == "yes");
Run Code Online (Sandbox Code Playgroud)