---
title: "Parellel.ForEach Loop"  
description: "In this blog I will show you a small demonstration of Parellel.ForEach loop. You can take advantage of multi-core processors and execute foreach loops"  
author: "Anonymous User"  
published: 2012-04-22  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/298/parellel-foreach-loop  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Parellel.ForEach Loop

In this blog I will show you a small demonstration of Parellel.[ForEach loop](https://www.mindstick.com/forum/182/problem-in-getting-the-value-from-array-by-using-foreach-loop). You can take advantage of multi-core [processors](https://answers.mindstick.com/qa/96913/name-of-the-latest-computer-processors-cpu) and execute foreach loops in parallel. This works especially well when getting [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) chunks of data from [external](https://www.mindstick.com/articles/12810/how-to-connect-tablet-to-external-monitor-or-flat-screen-tv-using-computer-adapters) data [sources](https://www.mindstick.com/articles/44491/how-to-evaluate-credibility-of-internet-sources) such as a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax), [web service](https://www.mindstick.com/articles/895/web-service-introduction), or [RESTful API](https://www.mindstick.com/articles/335974/retrieve-data-from-restful-api-and-add-paging-in-knockout-js).

Following example will show you [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) of Parellel.ForEach loop.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication1{    /// <summary>    /// A user defined datatype to store information of    /// student such as Name and ID.    /// </summary>    public class Student    {        public int ID { get; set; }        public string Name { get; set; }    }    class Program    {        static void Main(string[] args)        {            List<Student> StudentRecords = GetStudentRecords();            //Here we will display all records of student whose name start with A            //Using Parellel.ForEach loop.            Parallel.ForEach(StudentRecords, TempStudent =>                {                    if (TempStudent.Name.StartsWith("A"))                    {                        Console.WriteLine("Student ID : {0} , Name : {1}", TempStudent.ID, TempStudent.Name);                    }                });        }        /// <summary>        /// Returns a junk records of collection of        /// Student to perform operation.        /// </summary>        /// <returns></returns>        private static List<Student> GetStudentRecords()        {            List<Student> StudentList = new List<Student>();            StudentList.Add(new Student            {                ID = 1,                Name = "Awadhendra"            });            StudentList.Add(new Student            {                ID = 2,                Name = "Anuj"            });            StudentList.Add(new Student            {                ID = 3,                Name = "Ravi"            });            StudentList.Add(new Student            {                ID = 4,                Name = "Ashish"            });            StudentList.Add(new Student            {                ID = 5,                Name = "Atul"            });            StudentList.Add(new Student            {                ID = 6,                Name = "Shikha"            });            StudentList.Add(new Student            {                ID = 7,                Name = "Pooja"            });            StudentList.Add(new Student            {                ID = 8,                Name = "Amit"            });            StudentList.Add(new Student            {                ID = 9,                Name = "Nidhi"            });            StudentList.Add(new Student            {                ID = 10,                Name = "Neha"            });            return StudentList;        }    }}
```

##### Output of following code snippet is as follows.

[Student](https://www.mindstick.com/articles/157138/student-loan-default-wage-garnishment-and-student-loan) ID : 8 , Name : Amit

Student ID : 4 , Name : Ashish

Student ID : 5 , Name : Atul

Student ID : 1 , Name : Awadhendra

Student ID : 2 , Name : Anuj

Press any key to continue . . .

\

---

Original Source: https://www.mindstick.com/blog/298/parellel-foreach-loop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
