Getting ready

We will start off by looking at where the parallel foreach loop does not perform better than the standard foreach loop. For this, we will create a small list of 500 items and just iterate over the list, writing the items to the console window.

For the second example, which illustrates the power of the parallel foreach loop, we will use the same list and create a file for each item in the list. The power and benefit of the parallel foreach loop will be evident in the second example. You will need to add the using System.Diagnostics; and using System.IO; namespaces to run this recipe.

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.