Dec 25 2023
// Create a large array of numbers
int[] numbers = Enumerable.Range(1, 1000000).ToArray();
// Use PLINQ to find even numbers
var evenNumbers = numbers.AsParallel()
.Where(n => n % 2 == 0)
.ToArray();
// Do something with the even numbers
Console.WriteLine($"Found {evenNumbers.Length} even numbers.");
var longNames = names.AsParallel()
.WithDegreeOfParallelism(2)
.Where(name => name.Length > 5)
.ToList();
// Example data source: an array of file paths
string[] filePaths = { /* file paths go here */ };
// PLINQ query to process files in parallel
var query = from path in filePaths.AsParallel()
.Where(name => IsValidFile(path))
.Select(path);
// A ConcurrentBag to store the results
var concurrentBag = new ConcurrentBag<string>();
// Process the results in parallel and add them to the ConcurrentBag
query.ForAll(path => concurrentBag.Add(ProcessFile(path)));
// Do something with the concurrentBag if needed
Join 13,250+ subscribers to improve your .NET Knowledge.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.