forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticLocalFunctions.cs
More file actions
61 lines (51 loc) · 1.52 KB
/
StaticLocalFunctions.cs
File metadata and controls
61 lines (51 loc) · 1.52 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
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Text;
namespace ExploreCsharpEight
{
class StaticLocalFunctions
{
#region LocalFunction_Counting
public IEnumerable<int> Counter(int start, int end)
{
if (start >= end) throw new ArgumentOutOfRangeException("start must be less than end");
return localCounter();
IEnumerable<int> localCounter()
{
for (int i = start; i < end; i++)
yield return i;
}
}
#endregion
internal int LocalFunctionWithCapture()
{
var items = Counter(1, 10);
foreach(var item in items)
{
Console.WriteLine(item);
}
return 1;
}
internal int LocalFunctionWithNoCapture()
{
var items = StaticCounter(1, 10);
foreach (var item in items)
{
Console.WriteLine(item);
}
return 1;
}
#region LocalFunction_Static
public IEnumerable<int> StaticCounter(int start, int end)
{
if (start >= end) throw new ArgumentOutOfRangeException("start must be less than end");
return localCounter(start, end);
static IEnumerable<int> localCounter(int first, int endLocation)
{
for (int i = first; i < endLocation; i++)
yield return i;
}
}
#endregion
}
}