How to do it...

  1. Start off by creating two methods in the Demo class. Call one method ReadCollectionForEach() and pass it a parameter of List<string>. Create a second method called ReadCollectionParallelForEach() that also accepts a parameter of List<string>:
        public class Demo         {           public double ReadCollectionForEach(List<string> intCollection)           {                   }           public double ReadCollectionParallelForEach(List<string>             intCollection)           {                   }         }
  1. In the ReadCollectionForEach() method, add a standard foreach loop that will iterate over the collection of strings passed to it and write the value it finds to the console window. Then, clear the console window. Use a timer to keep track of the total seconds elapsed during the foreach loop:
 var timer = Stopwatch.StartNew(); ...

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.