-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (44 loc) · 1.34 KB
/
Copy pathProgram.cs
File metadata and controls
51 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace AsyncEnumerable
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Discovering AsyncEnumerable in C# 8.0");
Console.WriteLine("Step 1: Awaitable inside loop");
foreach (var dataPoint in await FetchIOTDataV1())
{
Console.WriteLine(dataPoint);
}
Console.WriteLine("Please press Enter to re-run the example using IAsyncEnumerable.");
Console.ReadLine();
Console.WriteLine("Step 2: IAsyncEnumerable");
await foreach (var dataPoint in FetchIOTDataV2())
{
Console.WriteLine(dataPoint);
}
Console.ReadLine();
}
static async Task<IEnumerable<int>> FetchIOTDataV1()
{
List<int> dataPoints = new List<int>();
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);
dataPoints.Add(i);
}
return dataPoints;
}
static async IAsyncEnumerable<int> FetchIOTDataV2()
{
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);
yield return i;
}
}
}
}