-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegateType.cs
140 lines (106 loc) · 4.54 KB
/
DelegateType.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
134
135
136
137
138
139
140
/*▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
• "FUNCTIONS" •
───────────────
• "DELEGATE TYPE" •
▬ "Delegations" in "C#"
→ are "Special Types" of "Objects"
→ that Allow "Functions"
→ to be "Treated" as "Objects".
♦ "They"
→ are "References"
→ to "Create" and "Manage Methods",
→ and Allow these references
→ to Be "Passed" as
•► "Parameters",
→ "Returned" from "Methods",
→ or "Stored"
→ in "Collections".
▬ "Key Points" about "Delegation":
1. "Method Pointers":
♦ "Delegations"
→ "Provide" a "Mechanism"
→ to "Refer"
→ to "Methods" as "Objects",
→ Allowing them
→ to be "Stored"
→ in "Variables",
→ "Passed" as "Parameters"
→ and "Returned"
→ from "Other Methods".
2. "Flexibility":
♦ "Delegations"
→ Allow "Instances"
→ to "Be Created"
→ to "Represent"
→ any "Value"
→ with a "Signature"
→ that "Matches"
→ the "Set Delegate".
♦ This "Flexibility"
→ allows "Delegation"
→ to be "Used"
→ to Define:
• "Anonymous Functions" or
• "Lambda Expressions",
→ which Makes It "Easier"
→ to "Work" with:
•► "Events",
•► "Callbacks"
→ and Other "Asynchronous Programming Patterns".
3. "Events":
♦ "Delegations" in "C#"
→ are "Common"
→ to "Implement"
→ the "Event Pattern".
♦ A "Class"
→ "Declares" an "Event",
→ which is "Actually"
→ a "Pair" of "Subscriber Add"
→ and "Remove Methods",
→ both of which
→ the "Delegate" can "Reach".
4. "Behavior Encapsulation":
♦ By "Using Delegations"
→ the "Logic" of a "Method"
→ can be "Encapsulated"
→ and "Passed"
→ as an "Argument",
→ Allowing "Other Parts" of the "Code"
→ to "Determine Exactly"
→ what "Behavior" to "Execute"
→ in a "Given Context2".
▬ Essentially, "Delegations"
→ "Enable" a "High Degree"
→ of "Flexibility"
→ and "Extensibility" in "C#",
→ allowing "Programmers"
→ to "Implement"
→ "Complex Programming Patterns"
→ such as:
•► "Callbacks",
•► "Events",
→ and "Other Forms"
→ of "Asynchronous Programming".
▬ "Lambda Expressions"
→ are "Anonymous Functions"
→ that can be "Treated"
→ as "Data Objects"
→ and "Assigned"
→ to "Delegate Variables".
♦ "They"
→ are "used" to Create "Inline Functions"
→ in a "Concise"
→ and "Expressive Manner".
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀*/
namespace Csharp.functions;
public class DelegateType
{
public static void RunDelegateType()
{
// ▼ "Variable" of "Delegate Type"
// → and Its "Assignment"
// → to a "Anonymous Function" ("Lambda Expression") ▼
Func<int, int, int> multiply = (x, y) => x * y;
Console.WriteLine("Delegate Type and its Assignment to a Lambda Expression - for Multiply: " + multiply(5, 10));
}
}