O'Reilly logo

C# 7 and .NET Core Cookbook by Dirk Strauss

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required