-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferred Execution 3.linq
More file actions
35 lines (29 loc) · 1.01 KB
/
Deferred Execution 3.linq
File metadata and controls
35 lines (29 loc) · 1.01 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
<Query Kind="Statements">
<Reference><ApplicationData>\LINQPad\Samples\Programming Reactive Extensions and LINQ\System.Reactive.dll</Reference>
<Namespace>System.Reactive</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
</Query>
/* Deferred Execution 3:
*
* We can fix the broken code above by adding a ToArray operator to the
* pipeline.
* this turns the evenNumbersInSeries from an IEnumerable to an array and the array is populated before
* the values are displayed
*/
int counter = 0;
var evenNumbersInSeries = Enumerable.Range(0, 10).Select(x => {
int result = x + counter;
counter++;
return result;
}).ToArray();
// List the numbers in the series
Console.WriteLine("First Try:\n");
foreach(int i in evenNumbersInSeries) {
Console.WriteLine(i);
}
// This time, because we added the ToArray(), we'll get the expected result
// every time.
Console.WriteLine("\nSecond Try:\n");
foreach(int i in evenNumbersInSeries) {
Console.WriteLine(i);
}