Parallel.For(): Update variable outside of loop
You can’t do this. sum is being shared across you parallel threads. You need to make sure that the sum variable is only being accessed by one thread at a time: // DON’T DO THIS! Parallel.For(0, data.Count, i => { Interlocked.Add(ref sum, data[i]); }); BUT… This is an anti-pattern because you’ve effectively serialised the loop … Read more