1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApp1 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Parallel.For(0, 15, 14 i => Console.WriteLine("The square of {0} is {1}", i, i * i)); 15 16 const int maxValues = 50; 17 int[] squares = new int[maxValues]; 18 19 Parallel.For(0, maxValues, 20 i => squares[i] = i * i); 21 22 for (var i = 0; i < maxValues; i++) 23 { 24 Console.WriteLine(squares[i]); 25 } 26 27 Console.WriteLine("=========================================="); 28 string[] words = new string[] { "We", "hold", "there", "to", "be", "self-evident" }; 29 30 Parallel.ForEach(words, 31 i => Console.WriteLine($"{i} has {i.Length}"));//和For的區別是:For須要索引號,ForEach不須要 32 33 /* 34 The square of 0 is 0 35 The square of 6 is 36 36 The square of 5 is 25 37 The square of 1 is 1 38 The square of 4 is 16 39 The square of 13 is 169 40 The square of 3 is 9 41 The square of 11 is 121 42 The square of 12 is 144 43 The square of 14 is 196 44 The square of 2 is 4 45 The square of 8 is 64 46 The square of 10 is 100 47 The square of 9 is 81 48 The square of 7 is 49 49 0 50 1 51 4 52 9 53 16 54 25 55 36 56 49 57 64 58 81 59 100 60 121 61 144 62 169 63 196 64 225 65 256 66 289 67 324 68 361 69 400 70 441 71 484 72 529 73 576 74 625 75 676 76 729 77 784 78 841 79 900 80 961 81 1024 82 1089 83 1156 84 1225 85 1296 86 1369 87 1444 88 1521 89 1600 90 1681 91 1764 92 1849 93 1936 94 2025 95 2116 96 2209 97 2304 98 2401 99 ========================================== 100 We has 2 101 self-evident has 12 102 to has 2 103 there has 5 104 hold has 4 105 be has 2 106 請按任意鍵繼續. . . 107 108 */ 109 } 110 } 111 }