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

Bra*_*d M 5 c# plinq

对于我的生活,我无法弄清楚为什么所有的foos都不为空.我假设.ForAll()应该在我调用.All()方法之前执行,但事实并非如此?

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 ??
}
Run Code Online (Sandbox Code Playgroud)

rec*_*ive 7

当你这样做

newFoos.ForAll(x => x = null);
Run Code Online (Sandbox Code Playgroud)

您要分配nullx,这是你的拉姆达的参数. x是lambda的本地人.它不是一个ref参数,为它赋值不会影响它的正文.实际上,这条线没有任何作用.