-
Notifications
You must be signed in to change notification settings - Fork 40
/
delegate_example.cs
93 lines (77 loc) · 2.38 KB
/
delegate_example.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
using System;
using System.Collections.Generic;
delegate void PrintMeInfo(int number);
delegate int Calculations(int first_number, int second_number, int multiplier);
class DelegateTest
{
public static int SumAndMultiply1(int A, int B, int C)
{
Console.WriteLine("Without brackets for {0}+{1}*{2}.",A,B,C);
return A + B * C;
}
public static int SumAndMultiply2(int A, int B, int C)
{
Console.WriteLine("With brackets for {0}+{1}*{2}.", A, B, C);
return (A + B) * C;
}
public static int SumAndMultiply3(int A, int B, int C)
{
Console.WriteLine("Just multiplying all of them.", A, B, C);
return A * B * C;
}
public void BiggerThanTen(int number)
{
string result = "";
if (number>10)
{
result = "Bigger than 10.";
} else {
result = "Not bigger than 10.";
}
Console.WriteLine(result);
}
public static void IsOdd(int number)
{
string result = "";
if (number % 2 == 1)
{
result = "It is odd.";
} else {
result = "It is not odd";
}
Console.WriteLine(result);
}
public static void IsDividableByTen (int number)
{
string result = "";
if (number % 10 == 0)
{
result = "Number is dividable by ten.";
} else {
result = "Number is not dividable by ten.";
}
Console.WriteLine(result);
}
public static void ShowMeTheNumber (int number)
{
Console.WriteLine("The number is {0}.",number);
}
static void Main()
{
PrintMeInfo info = null;
info += new PrintMeInfo(ShowMeTheNumber);
info += new PrintMeInfo(DelegateTest.IsOdd);
info += new PrintMeInfo(IsDividableByTen);
DelegateTest additional_info = new DelegateTest();
info += new PrintMeInfo(additional_info.BiggerThanTen); //The method is not static!
Calculations calc = null;
calc += new Calculations(DelegateTest.SumAndMultiply1);
calc += new Calculations(DelegateTest.SumAndMultiply2);
List<Calculations> my_list = new List<Calculations> { SumAndMultiply1, SumAndMultiply2 , SumAndMultiply3};
foreach (var my_result in my_list)
{
Console.WriteLine(my_result(5,6,7));
}
info(25);
}
}