Erg, I\'m trying to find these two methods in the BCL using Reflector, but can\'t locate them. What\'s the difference between these two snippets?
A:
The difference is, B isn't parallel. The only thing AsParallel()
does is that it wraps around a IEnumerable
, so that when you use LINQ methods, their parallel variants are used. The wrapper's GetEnumerator()
(which is used behind the scenes in the foreach
) even returns the result of the original collection's GetEnumerator()
.
BTW, if you want to look at the methods in Reflector, AsParallel()
is in the System.Linq.ParallelEnumerable
class in the System.Core
assembly. Parallel.ForEach()
is in the mscorlib
assembly (namespace System.Threading.Tasks
).