forked from ProfessionalCSharp/ProfessionalCSharp7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
133 lines (119 loc) · 3.65 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Runtime.CompilerServices;
namespace LocalFunctions
{
class Program
{
static void Main()
{
IntroWithLambdaExpression();
IntroWithLocalFunctions();
IntroWithLocalFunctionsWithExpressionBodies();
IntroWithLocalFunctionsWithClosures();
YieldSampleSimple();
YieldSampleWithPrivateMethod();
YieldSampleWithLocalFunction();
SortSample();
Algorithms.WhenDoesItEnd();
}
private static void IntroWithLambdaExpression()
{
Func<int, int, int> add = (x, y) =>
{
return x + y;
};
int result = add(37, 5);
Console.WriteLine(result);
}
static void IntroWithLocalFunctions()
{
int add(int x, int y)
{
return x + y;
}
int result = add(37, 5);
Console.WriteLine(result);
}
private static void IntroWithLocalFunctionsWithExpressionBodies()
{
int add(int x, int y) => x + y;
int result = add(37, 5);
Console.WriteLine(result);
}
private static void IntroWithLocalFunctionsWithClosures()
{
int z = 3;
int result = add(37, 5);
Console.WriteLine(result);
int add(int x, int y) => x + y + z;
}
private static void YieldSampleSimple()
{
#line 1000
Console.WriteLine(nameof(YieldSampleSimple));
try
{
string[] names = { "James", "Niki", "John", "Gerhard", "Jack" };
var q = names.Where1(null);
foreach (var n in q) // callstack position for exception
{
Console.WriteLine(n);
}
}
catch (ArgumentNullException ex)
{
Console.WriteLine(ex);
}
Console.WriteLine();
}
private static void SortSample()
{
Console.WriteLine(nameof(SortSample));
int[] elements = { 7, 3, 4, 9, 11, 99, 22, 3, 5, 7, 12, 14, 97, 33, 343 };
Algorithms.QuickSort(elements);
foreach (var item in elements)
{
Console.WriteLine(item);
}
Console.WriteLine();
}
private static void YieldSampleWithPrivateMethod()
{
#line 1000
Console.WriteLine(nameof(YieldSampleWithPrivateMethod));
try
{
string[] names = { "James", "Niki", "John", "Gerhard", "Jack" };
var q = names.Where2(null); // callstack position for exception
foreach (var n in q)
{
Console.WriteLine(n);
}
}
catch (ArgumentNullException ex)
{
Console.WriteLine(ex);
}
Console.WriteLine();
}
private static void YieldSampleWithLocalFunction()
{
#line 1000
Console.WriteLine(nameof(YieldSampleWithLocalFunction));
try
{
string[] names = { "James", "Niki", "John", "Gerhard", "Jack" };
var q = names.Where3(null); // callstack position for exception
foreach (var n in q)
{
Console.WriteLine(n);
}
}
catch (ArgumentNullException ex)
{
Console.WriteLine(ex);
}
Console.WriteLine();
}
}
}