-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnonymousFunctionsAndLambdaExpressions.cs
104 lines (71 loc) · 3.69 KB
/
AnonymousFunctionsAndLambdaExpressions.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
/*▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
• "FUNCTIONS" •
───────────────
• "ANONYMOUS FUNCTION" OR
"LAMBDA EXPRESSION" •
▬ An "Anonymous" Function ib "C#"
→ is a "Function Without" a "Specific Name",
→ "Defined" in a "Context2"
→ where a "Function" is "Needed",
→ but does "Not Need"
→ to be "Explicitly Defined"
→ as a "Separate Method".
♦ An "Anonymous" Function
→ can be "Created" using the
•► "delegate" Keyword or using
•► "Lambda Expressions".
▬ "Lambda Expressions"
→ are a "Special Form"
→ of "Anonymous Functions" in "C#".
♦ "They"
→ are "Useful" for "Defining Functions"
→ in a "Concise" and "Expressive Way".
♦ The "Syntax" of a "Lambda Expression"
───────────────────────────────────────────────────────────
// ▼ "Expression Lambda" ▼
(parameter_list) => expression
// ▼ "Statement Lambda" ▼
() => { statement_block }
───────────────────────────────────────────────────────────
→ is "Short"
→ and "Focuses" on the "Parameters"
→ and the "Function Body".
▬ "Lambda Expressions"
→ are "Often Used"
→ in "Combination"
→ with "High-Order Functions" such as:
•► "Select",
•► "Where",
•► "Aggregate", etc.,
→ within "LINQ" ("Language Integrated Query")
→ or in "Other Contexts"
→ where "Anonymous Functions"
→ are "Required"
→ to "Implement Specific Behaviors".
♦ Using "Lambda Expressions" in "C#"
→ makes "Code" more "Concise"
→ and "Easier" to "Understand"
→ in many "Situations".
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀*/
namespace Csharp.functions;
public class AnonymousFunctionsAndLambdaExpressions
{
public static void RunAnonymousFunctionAndLambdaExpressions()
{
// 1- Using a "Expression Lambda":
Action expressionLambda = () => Console.WriteLine("Expression Lambda: " + "Hello, World!");
expressionLambda();
Console.WriteLine();
Action<string> expressionLambda2 = (s) => Console.WriteLine(s);
expressionLambda2("Expression Lambda with String Type (s)");
Console.WriteLine();
// 2- Using a "Statement Lambda":
Action statementLambda = () =>
{
Console.WriteLine("Statement Lambda: " + "Hello, World Again!");
Console.WriteLine("I am Marius");
Console.WriteLine("We will learn together C#");
};
statementLambda();
}
}