-
Notifications
You must be signed in to change notification settings - Fork 64
/
Program.cs
52 lines (47 loc) · 1.3 KB
/
Program.cs
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
52
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp4
{
static class Program
{
static void Main()
{
YieldDemo();
//var numbers = new[] { 1, 2, 3, 4, 5 };
var numbers = new[] { "1", "2", "10" };
foreach(var number in numbers.Filter(n => n.StartsWith("1")))
//foreach(var number in numbers.Where(n => n.StartsWith("1")))
{
Console.WriteLine(number);
}
}
#region Yield demo
static void YieldDemo()
{
// Note how the values are generated using `yield`
foreach(var x in GenerateValues())
{
Console.WriteLine(x);
}
}
static IEnumerable<int> GenerateValues()
{
yield return 1;
yield return 2;
yield return 3;
}
#endregion
// Note how `Filter` is generic so that it works with any type
static IEnumerable<T> Filter<T>(this IEnumerable<T> input, Func<T, bool> filterFunction)
{
foreach(var item in input)
{
if (filterFunction(item))
{
yield return item;
}
}
}
}
}