-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
103 lines (77 loc) · 3.37 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
using System;
namespace Program2
{
class Program
{
delegate int Transformer(int i);
delegate int Calculator(int x, int y);
static void Main(string[] args)
{
// Local static method
static int Square(int x) => x * x;
static int Div(int x, int y)
{
int result = 0;
// Exception handle
try
{
result = x / y;
}
catch (DivideByZeroException)
{
Console.WriteLine("DIVISION BY ZERO IS UNDEFINED.");
}
return result;
}
// ----------------------- LAMBDA EXPRESSIONS ---------------------------
// A lambda expression is an unnamed method written in place of a delegate instance.
// Named method Square
Func<int, int> sqr = Square;
// Unnamed method: Lambda expressions (expression)
sqr += x => x * x;
// Unnamed method: Lambda expressions (statement block)
sqr += x => { return x * x; };
// Run many methods at the same time
Delegate[] delegateList1 = sqr.GetInvocationList();
foreach (Func<int, int> instance in delegateList1)
{
Console.WriteLine("Square of 1: {0}", instance(1));
Console.WriteLine("Square of 2: {0}", instance(2));
Console.WriteLine("Square of 3: {0}", instance(3));
Console.WriteLine();
}
// Unnamed method: Lambda expressions (expression)
Func<int, int, int> calculator = (x, y) => x + y; // The parenthesis is needed for multiple parameters
// Unnamed method: Lambda expressions (statement block)
calculator += (x, y) => { return x - y; };
// Anonymous Methods
calculator += delegate (int x, int y) { return x * y; };
// Named method Div
calculator += Div;
// Run many methods at the same time
Delegate[] delegateList2 = calculator.GetInvocationList();
foreach (Func<int, int, int> instance in delegateList2)
{
Console.WriteLine(instance(10, 2));
}
Console.WriteLine();
foreach (Func<int, int, int> instance in delegateList2)
{
Console.WriteLine(instance(5, 0));
}
Console.WriteLine();
// Definition:
// Outer variables referenced by a lambda expression are called captured variables.
// A lambda expression that captures variables is called a closure.
int scalar = 2; // Captured variable (outer variable)
Func<int, int> multiplier = n => n * scalar;
// Static Lambda (New feature in C# 9.0)
// Try to run the following code: will not compile
// Func<int, int> multiplier = static n => n * scalar;
Console.WriteLine(multiplier(3));
scalar = 10; // Invoked value
Console.WriteLine(multiplier(3));
Console.WriteLine("The output is 30 instead of 6 because captured variables are evaluated when the delegate is actually invoked, not when the variables were captured.");
}
}
}