C# 并行LINQ-.Select()+。ForAll()返回奇怪的结果

C# 并行LINQ-.Select()+。ForAll()返回奇怪的结果,c#,plinq,C#,Plinq,就我而言,我不明白为什么所有的foo都不是空的。 我假设在调用.All()方法之前,应该先执行.ForAll()方法,但不是吗 public class Foo { public string Bar { get; set; } } static void Main(string[] args) { var foos = new List<Foo> { new Foo(), new Foo(), new Foo() }; var newFoos = foos

就我而言,我不明白为什么所有的foo都不是空的。 我假设在调用
.All()
方法之前,应该先执行
.ForAll()
方法,但不是吗

public class Foo
{
    public string Bar { get; set; }
}

static void Main(string[] args)
{
    var foos = new List<Foo> { new Foo(), new Foo(), new Foo() };
    var newFoos = foos
        .AsParallel()
        .Select(x =>
        {
            x.Bar = "";
            return x;
        });
    newFoos.ForAll(x => x = null);
    var allFoosAreNull = newFoos.All(x => x == null);
    Console.WriteLine(allFoosAreNull); // False ??
}
公共类Foo
{
公共字符串条{get;set;}
}
静态void Main(字符串[]参数)
{
var foos=新列表{new Foo(),new Foo(),new Foo()};
var newFoos=foos
.天冬酰胺()
.选择(x=>
{
x、 Bar=“”;
返回x;
});
newFoos.ForAll(x=>x=null);
var allFoosAreNull=newFoos.All(x=>x==null);
Console.WriteLine(allFoosAreNull);//False??
}
当您执行此操作时

newFoos.ForAll(x => x = null);

您正在将
null
赋值给
x
,这是lambda的参数<代码>x是lambda的本地代码。它不是
ref
参数,为其赋值在其主体之外没有任何效果。实际上,该行不起任何作用。

即使我修改lambda的主体以将
Bar
属性设置为另一个值,该值也不会保持不变。我还是有点困惑;我假设
ForAll()
正在实际列表上迭代。换句话说,为什么lambda不是
ref
?现在我想起来了,这与PLINQ没有任何关系……它与
.ForEach()的行为相同。