C#学习笔记二:并行编程基础:在 PLINQ 和 TPL 中的 Lambda 表达式

任务并行库 (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)

 

 

posted @ 2014-01-12 22:58  sherlock99  阅读(266)  评论(0编辑  收藏  举报