Parallel.ForEach, PLINQ
Run work in parallel with Parallel.ForEach and build parallel pipelines with PLINQ (AsParallel, WithDegreeOfParallelism, AsOrdered).
Parallel.ForEach, PLINQ is a free C# Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
Why parallel loops
Goal: Use the CPU better.
- Parallel.ForEach: action per item
- PLINQ: parallel query with operators
- Control degree of parallelism
- Be careful with side-effects
Parallel.ForEach basics
Parallel.ForEach spreads iterations across threads; each item runs the given action.
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main(string[] args)
{
int[] data = new int[] { 1, 2, 3, 4, 5, 6 };
// Runs iterations concurrently on thread pool
Parallel.ForEach(data, x =>
{
int y = x * x; // small CPU work
Console.WriteLine("x=" + x + " y=" + y + " on " + Environment.CurrentManagedThreadId);
});
}
}
Control parallelism
Use ParallelOptions.MaxDegreeOfParallelism to limit concurrency when resources are scarce.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
public static void Main(string[] args)
{
int[] data = new int[] { 1, 2, 3, 4, 5, 6 };
ParallelOptions opt = new ParallelOptions();
opt.MaxDegreeOfParallelism = 2; // limit concurrency
Parallel.ForEach(data, opt, x =>
{
Console.WriteLine("Start " + x);
Thread.Sleep(150); // simulate work
Console.WriteLine("Done " + x);
});
}
}
PLINQ pipeline
PLINQ runs LINQ operators in parallel. ForAll consumes results concurrently (unordered).
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] nums = Enumerable.Range(1, 20).ToArray();
// Filter then project in parallel
var query =
nums
.AsParallel()
.WithDegreeOfParallelism(2)
.Where(n => n % 2 == 0)
.Select(n => n * n);
// ForAll consumes in parallel; order not guaranteed
query.ForAll(sq => Console.WriteLine(sq));
}
}
Ordering controls
Use AsOrdered to preserve order; AsSequential switches back to single-threaded LINQ when needed.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] nums = new int[] { 5, 4, 3, 2, 1 };
// Preserve original sequence order for the output
var ordered =
nums.AsParallel().AsOrdered().Select(n => n * 10).ToArray();
// Switch back to sequential for a section if needed
var seq =
nums.AsParallel().Select(n => n + 1).AsSequential().Where(n => n > 3).ToArray();
Console.WriteLine("Ordered: " + string.Join(",", ordered));
Console.WriteLine("Sequential tail: " + string.Join(",", seq));
}
}
Safety & tips
Tips:
- Avoid shared mutable state; prefer pure functions.
- Use thread-safe collections if you must write.
- Do larger batches instead of tiny work items.
- Measure: parallelism helps CPU-bound work, not I/O waiting.
Parallel.ForEach vs PLINQ
Recap
Recap: Use Parallel.ForEach for per-item actions; use PLINQ to build parallel queries, control concurrency, and optionally preserve order.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Parallel.ForEach, PLINQ” lesson free?
Yes — the full text of “Parallel.ForEach, PLINQ” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 3 lessons in total.
What will I learn in “Parallel.ForEach, PLINQ”?
Run work in parallel with Parallel.ForEach and build parallel pipelines with PLINQ (AsParallel, WithDegreeOfParallelism, AsOrdered). You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Parallel.ForEach, PLINQ” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Parallel.ForEach, PLINQ
- Producer/consumer with Channels (overview)
- Throughput vs latency trade-offs