Nav*_*adi 0 c# linq-to-objects anonymous-methods
我想在扩展方法的函数中修改局部变量.看到
int myvar=0;
MyList.Where(
x =>
{
if (condition)
myvar += 1;
return false;
});
return myvar;
Run Code Online (Sandbox Code Playgroud)
为什么那不起作用?
你真的不希望在体内修改局部变量Where谓词.像这样的副作用的功能是坏消息; 试着想象如果这来自(例如)由并行可枚举的生成会发生什么AsParallel()- 你将有竞争条件.
如果你解释一下你想要完成什么,我相信我们其中一个人可以为此提供更好的方法.我的猜测是它看起来像这样:
int count = myList.Count(x => condition(x));
Run Code Online (Sandbox Code Playgroud)