C#: running tasks in parallel with Task.WhenAll

A Task represents work that is running or will run, and Task<T> will eventually produce a value of type T. Task.Run schedules a delegate on a thread-pool thread, so two calls can run at the same time on different cores.

C#
using System;
using System.Linq;
using System.Threading.Tasks;

class Program {
  static async Task Main() {
    int[] data = Enumerable.Range(1, 1000).ToArray();

    Task<long> first = Task.Run(() => data.Take(500).Sum(x => (long)x));
    Task<long> second = Task.Run(() => data.Skip(500).Sum(x => (long)x));

    long[] parts = await Task.WhenAll(first, second);
    Console.WriteLine(parts[0] + " " + parts[1] + " " + parts.Sum());
  }
}
Read the full lesson