using System;
|
02
|
using System.IO;
|
03
|
using System.Linq;
|
04
|
using System.Threading.Tasks;
|
05
|
06
|
class Program
|
07
|
{
|
08
|
static void Main()
|
09
|
{
|
10
|
//
Return a value type with a lambda expression
|
11
|
Task< int >
task1 = Task< int >.Factory.StartNew(()
=> 1);
|
12
|
int i
= task1.Result;
|
13
|
14
|
//
Return a named reference type with a multi-line statement lambda.
|
15
|
Task<Test>
task2 = Task<Test>.Factory.StartNew(() =>
|
16
|
{
|
17
|
string s
= ".NET" ;
|
18
|
double d
= 4.0;
|
19
|
return new Test
{ Name = s, Number = d };
|
20
|
});
|
21
|
Test
test = task2.Result;
|
22
|
23
|
//
Return an array produced by a PLINQ query
|
24
|
Task< string []>
task3 = Task< string []>.Factory.StartNew(()
=>
|
25
|
{
|
26
|
string path
= @"C:\users\public\pictures\" ;
|
27
|
string []
files = Directory.GetFiles(path);
|
28
|
29
|
var
result = (from file in files.AsParallel()
|
30
|
let
info = new FileInfo(file)
|
31
|
where
info.Extension == ".jpg"
|
32
|
select
file).ToArray();
|
33
|
34
|
return result;
|
35
|
});
|
36
|
37
|
foreach (var
name in task3.Result)
|
38
|
Console.WriteLine(name);
|
39
|
40
|
}
|
41
|
class Test
|
42
|
{
|
43
|
public string Name
{ get ; set ;
}
|
44
|
public double Number
{ get ; set ;
}
|
45
|
46
|
}
|
47
|
}
|