On this article, we might be specializing in the parallel Foreach loop within the C# programming language. The phrase parallel in C# programming comes from the idea of threading duties, which signifies that after we are performing a couple of process concurrently, we are able to use the threading process library to name for the Job parallel library to get parallel for, Foreach loops to work over these duties as they’re extra environment friendly and fewer time consuming as in comparison with regular sequential for and Foreach loops. The Parallel Foreach loop divides the duties and runs the iterations in a couple of stream to get to the top purpose. The Parallel Foreach is simply used when a course of is iteratively known as for enter and is co-dependent on different operations because it will increase the tempo of processes.

Now we’ll carry out a number of examples to make clear and improve comprehension of the Parallel Foreach loop within the C# programming language.

Instance 01: Including Numbers to a Checklist Utilizing the Parallel Foreach Loop in Ubuntu 20.04

On this instance, we’ll write a C# program so as to add numbers to a listing utilizing the parallel Foreach loop and mark the variety of threads concurrently assigned to every step. We must use completely different libraries just like the threading process and Hyperlink to entry the monitoring and effectivity of the parallel for every loop. We might be performing this instance within the Ubuntu 20.04 atmosphere.g

Within the above code, now we have generated a listing utilizing the parallel for every loop through which the numbers have been assigned to the checklist with the assistance of the Vary perform. Then we additionally printed the thread ids at each step to watch the variety of threads used whereas assigning values to the checklist. After working this program file in our command line terminal, we’ll get the next output:

The output means that the checklist was assigned 5 values, and through this course of, two threads have been used, which created a time distinction in executing this process. If we are able to use a easy sequential Foreach loop, the duty would have been completed in a single thread, and the execution could be better than the execution time of the parallel Foreach loop. Though this may differ because it is dependent upon the scale of the duty at hand because the parallel foreach loop is extra handy for the bigger dimension of the duty and processing as a result of it divides the duty into a number of threads and works on the “divide and conquer” method.

Instance 02: Controlling Variety of Threads Utilizing Parallel Foreach Loop in Ubuntu 20.04

On this instance, we might be controlling the creation of the variety of threads when a parallel Foreach loop is executed. The duty threading library provides us entry to a property labeled “Max Diploma Of Parallelism” that controls the parallel operations of the Parallel Choices class and assigning a worth to this property defines the variety of parallel operations to be executed throughout this system. This property is distributed to the parallel Foreach loop via an argument. We are going to create a C# program to assign numbers to a listing and outline the variety of threads for executing this process.

As we are able to see that the full variety of threads utilized in executing this process was solely 2, as we fastened this quantity by assigning worth to the “Max Diploma of Parallelism.” In case of an enormous quantity of knowledge processing, we are able to differ the worth assigned to the Max Diploma of Parallelism; it will assist us divide the processing energy equally and effectively among the many sub-tasks or parallel operations.

Instance 03: Printing Checklist with String Variables Utilizing the Parallel Foreach Loop in a C# Program in Ubuntu 20.04

On this instance, we might be printing gadgets of a listing with string variables by utilizing the parallel Foreach loop in a C# program within the Ubuntu 20.04 atmosphere. We are going to first initialize a listing and assign values to it manually after which print all its components because the output utilizing the parallel foreach loop.

Within the above code, we made a listing named continents and added names of the continents to the checklist earlier than utilizing the parallel for every loop. We added the threading process library as a way to use the parallel Foreach loop for our code. The parallel foreach technique takes within the checklist and a brand new object as a parameter to retailer the values quickly for every iteration. After executing this code file on our command line terminal, we’ll get the checklist of the names of the continents that we saved earlier in our checklist.

Instance 04: Evaluating Conventional Foreach Loop with Sequential Utilizing Parallel Foreach Loop in C# in Ubuntu 20.04

On this instance, we might be evaluating the standard Foreach loop with its sequential property with the parallel Foreach loop by doing an analogous process with each the loops. The execution time of each the loops might be completely different due to their completely different method to the duty. We are going to print the contents of the checklist, which might be initialized earlier than the operation, utilizing the standard Foreach loop with its sequential property and the parallel for every loop.

Within the above C# program, we’re utilizing a traditional Foreach loop and a parallel Foreach loop and calculating the time taken to execute the duty by each the loops; we’ll use the stopwatch class from the C# programming language’s “Diagnostics” library. The stopwatch class has a “StartNew()” technique, which begins the stopwatch, and the elapsed() perform is used to cease the stopwatch. Each these capabilities have been made accessible by making an object of the StopWatch class.

The output exhibits that the standard Foreach loop took much less time in executing the duty than the Parallel Foreach loop. The reason being that the execution of the duty is greatest suited to a single thread operation, and creating a number of threads for this process could be time-consuming, but when the checklist would have comprised hundreds of knowledge, then the execution time could be vice versa as a result of in that situation threading the duty could be environment friendly and time-saving. So, when utilizing the Parallel Foreach loop, now we have to watch the scale of the processing energy required for that operation and think about which would be the appropriate loop for that operation.


We mentioned the parallel Foreach loop within the C# programming language. On this article, the which means of the identify “Parallel” was mentioned, in addition to the sensible variations between a parallel Foreach loop and an ordinary Foreach loop. Then we applied a number of examples of the Parallel Foreach loop within the Ubuntu 20.04 atmosphere to know extra readability on the subject. We concluded that utilizing a parallel foreach loop must be monitored whereas programming as it might go both means as a result of its threading mechanism; the duty is split, which might be time-consuming within the case of a smaller process.

Did you want this text?
Share it on any of the next social media channels beneath to provide us your vote. Your suggestions helps us enhance.