任务并行库 (TPL) 包含许多方法,这些方法采用委托的 System.Func<TResult> 或 System.Action 系列中的其中一个作为输入参数。
Func 委托
Func 委托封装一个返回值的方法。 在 Func 签名中,最后或最右侧的类型参数始终指定返回类型。Framework 类库定义了 17 个从没有类型参数的版本直至具有 16 个类型参数的版本。
System.Func<TResult>、System.Func<T, TResult>、System.Func<T1, T2, TResult>
(n, loopState, localSum) => { localSum += n; Console.WriteLine("Thread={0}, n={1}, localSum={2}", Thread.CurrentThread.ManagedThreadId, n, localSum); return localSum; }
Action 委托
System.Action 委托封装一个不返回值的方法。 在 Action 类型签名中,类型参数仅表示输入参数。
(localSum) => Interlocked.Add(ref sum, localSum)