-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegatesDemo.cs
85 lines (68 loc) · 2.56 KB
/
DelegatesDemo.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
using System;
using System.Collections.Generic;
using System.Text;
namespace C_Sharp
{
class DelegatesDemo
{
public delegate double PerformCalculation(double x, double y);
public static double AddTwoNumbers(double a, double b)
{
Console.WriteLine("{0}+{1}={2}",a,b, a + b);
return a + b;
}
public static double SubTwoNumbers(double a, double b)
{
Console.WriteLine("{0}-{1}={2}",a,b, a - b);
return a - b;
}
public static double MulTwoNumbers(double a, double b)
{
Console.WriteLine("{0}*{1}={2}", a, b, a * b);
return a * b;
}
public static void ExplainDelegates()
{
PerformCalculation performCalculation = AddTwoNumbers;
Console.WriteLine(performCalculation(10, 21.9));
performCalculation = SubTwoNumbers;
Console.WriteLine(performCalculation(20, 10));
PerformCalculation getSum = AddTwoNumbers;
PerformCalculation getSub = SubTwoNumbers;
PerformCalculation getMul = MulTwoNumbers;
PerformCalculation performCalculation1 = getSum+ getSub;
performCalculation1 += getMul;
Console.WriteLine(performCalculation1(20, 10));
//result at the end which method is called
performCalculation1 -= getSub;
Console.WriteLine(performCalculation1(20, 10));
}
public delegate string GetTextDelegate(string name);
public static void ExplainAnonymousMethod()
{
GetTextDelegate getTextDelegate = delegate (string name)
{
return "Hello " + name;
};
Console.WriteLine(getTextDelegate("Bhavik"));
Display(getTextDelegate);
}
public static void Display(GetTextDelegate getTextDelegate)
{
Console.WriteLine(getTextDelegate("Bhavik"));
}
public static void ExplainLambdaExpressions()
{
//Expression Lambda
GetTextDelegate getTextDelegate = (string name) =>{ return "Hello " + name; };
//Statement Lambda
GetTextDelegate getTextDelegate1 = (string name) => {
string value = "Hello " + name + " Good morning";
return value;
};
Console.WriteLine(getTextDelegate("Raj"));
PerformCalculation performCalculation = (a, b) => (a + b);
Console.WriteLine(performCalculation(10, 20));
}
}
}