-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod_as_parameter.cs
69 lines (59 loc) · 2.31 KB
/
method_as_parameter.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
using System;
class SendingMethodToMethodDemo
{
static void Main()
{
// Sending Named methods to ReceiverMethod()
ReceiverMethod(SquareMethod, "Named");
ReceiverMethod(IsEvenMethod, "Named");
ReceiverMethod(PrintMessageMethod, "Named");
// Sending Anonymous methods to ReceiverMethod
ReceiverMethod(delegate (int x) { return x * x; }, "Anonymous");
ReceiverMethod(delegate (int x) { return x % 2 == 0; }, "Anonymous");
ReceiverMethod(delegate (string s) { Console.Write(s); }, "Anonymous");
// Sending methods using Lambda expression
ReceiverMethod(x => x * x, "Lambda");
ReceiverMethod(x => x % 2 == 0, "Lambda");
ReceiverMethod(s => Console.Write(s), "Lambda");
}
// Named method for demonstration of sending method via Func
static int SquareMethod(int x)
{
return x * x;
}
// Named method for demostration of sending method via Predicate
static bool IsEvenMethod(int x)
{
return x % 2 == 0;
}
// Named method for demonstration of sending method via Action
static void PrintMessageMethod(string msg)
{
Console.Write(msg);
}
// Overloaded Method to execute a Func<int, int> type method
static void ReceiverMethod(Func<int, int> parcelMethod, string parcelType)
{
Console.Write("Received method using " + parcelType);
Console.Write(parcelType[0]=='L'? " expression" : " method");
Console.Write("-Func: Square of 5 = ");
int result = parcelMethod(5);
Console.WriteLine(result);
}
// Overloaded Method to execute a Predicate<int> type method
static void ReceiverMethod(Predicate<int> parcelMethod, string parcelType)
{
Console.Write("Received method using " + parcelType);
Console.Write(parcelType[0]=='L'? " expression" : " method");
Console.Write("-Predicate: Is 4 Even = ");
bool result = parcelMethod(4);
Console.WriteLine(result);
}
// Overloaded Method to execute an Action<string> type method
static void ReceiverMethod(Action<string> parcelMethod, string parcelType)
{
parcelMethod("Received method using " + parcelType);
Console.Write(parcelType[0]=='L'? " expression" : " method");
Console.WriteLine("-Action");
}
}