description | title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
---|---|---|---|---|---|---|---|---|---|---|
Learn more about: Lambda Expressions in PLINQ and TPL |
Lambda Expressions in PLINQ and TPL |
03/30/2017 |
|
|
645b2c17-29d0-4ffa-8684-430743cc2f2d |
The Task Parallel Library (TPL) contains many methods that take one of the xref:System.Func%601?displayProperty=nameWithType or xref:System.Action?displayProperty=nameWithType family of delegates as input parameters. You use these delegates to pass in your custom program logic to the parallel loop, task or query. The code examples for TPL as well as PLINQ use lambda expressions to create instances of those delegates as inline code blocks. This topic provides a brief introduction to Func and Action and shows you how to use lambda expressions in the Task Parallel Library and PLINQ.
Note
For more information about delegates in general, see Delegates and Delegates. For more information about lambda expressions in C# and Visual Basic, see Lambda Expressions and Lambda Expressions.
A Func
delegate encapsulates a method that returns a value. In a Func
signature, the last, or rightmost, type parameter always specifies the return type. One common cause of compiler errors is to attempt to pass in two input parameters to a xref:System.Func%602?displayProperty=nameWithType; in fact this type takes only one input parameter. .NET defines 17 versions of Func
: xref:System.Func%601?displayProperty=nameWithType, xref:System.Func%602?displayProperty=nameWithType, xref:System.Func%603?displayProperty=nameWithType, and so on up through xref:System.Func%6017?displayProperty=nameWithType.
A xref:System.Action?displayProperty=nameWithType delegate encapsulates a method (Sub in Visual Basic) that does not return a value. In an Action
type signature, the type parameters represent only input parameters. Like Func
, .NET defines 17 versions of Action
, from a version that has no type parameters up through a version that has 16 type parameters.
The following example for the xref:System.Threading.Tasks.Parallel.ForEach%60%602%28System.Collections.Generic.IEnumerable%7B%60%600%7D%2CSystem.Func%7B%60%601%7D%2CSystem.Func%7B%60%600%2CSystem.Threading.Tasks.ParallelLoopState%2C%60%601%2C%60%601%7D%2CSystem.Action%7B%60%601%7D%29?displayProperty=nameWithType method shows how to express both Func and Action delegates by using lambda expressions.
[!code-csharpSystem.Threading.Tasks.Parallel#02] [!code-vbSystem.Threading.Tasks.Parallel#02]