using System.Diagnostics;
using System.Threading.Tasks;
public static void Main()
"1. Red", "2. Green", "3. Blue", "4. Yellow", "5. White", "6. Black", "7. Violet", "8. Brown", "9. Orange", "10. Pink"
Console.WriteLine("Using Parallel.ForEach");
var sw = Stopwatch.StartNew();
Parallel.ForEach(colors, color =>
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", sw.Elapsed.TotalSeconds);
Console.WriteLine("\nTraditional foreach loop");
sw = Stopwatch.StartNew();
foreach (string color in colors)
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("foreach loop execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);