⚙️ C# / .NET Intermediate

What is the Task Parallel Library (TPL) in C#?

Answer

The Task Parallel Library (TPL) is a set of APIs in the System.Threading.Tasks namespace that simplifies parallel and asynchronous programming. Key components: Task: represents an asynchronous operation. Task<T>: returns a value. Task.Run(): queues work to the ThreadPool. Task.WhenAll(): await multiple tasks in parallel. Task.WhenAny(): complete when first task finishes. Parallel.For/ForEach: data parallelism with automatic partitioning across cores: Parallel.ForEach(items, item => Process(item));. PLINQ: parallel LINQ — items.AsParallel().Where(x => Expensive(x)).ToList(). CancellationToken: cooperative cancellation — pass to async methods and check in loops. IProgress<T>: report progress back to the UI thread. SemaphoreSlim: async-friendly throttling. The TPL abstracts thread management, work stealing, and load balancing.