-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_50.cs
99 lines (86 loc) · 2.44 KB
/
lesson_50.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
class Program
{
delegate int Operation(int x, int y);
static void Main(string[] args)
{
Operation operation = (x, y) => x + y;
Console.WriteLine(operation(10, 20)); // 30
Console.WriteLine(operation(40, 20)); // 60
Console.Read();
}
}
class Program
{
delegate int Square(int x); // объявляем делегат, принимающий int и возвращающий int
static void Main(string[] args)
{
Square square = i => i * i; // объекту делегата присваивается лямбда-выражение
int z = square(6); // используем делегат
Console.WriteLine(z); // выводит число 36
Console.Read();
}
}
class Program
{
delegate void Hello(); // делегат без параметров
static void Main(string[] args)
{
Hello hello1 = () => Console.WriteLine("Hello");
Hello hello2 = () => Console.WriteLine("Welcome");
hello1(); // Hello
hello2(); // Welcome
Console.Read();
}
}
class Program
{
delegate void ChangeHandler(ref int x);
static void Main(string[] args)
{
int x = 9;
ChangeHandler ch = (ref int n) => n = n * 2;
ch(ref x);
Console.WriteLine(x); // 18
Console.Read();
}
}
class Program
{
delegate void Hello(); // делегат без параметров
static void Main(string[] args)
{
Hello message = () => Show_Message();
message();
}
private static void Show_Message()
{
Console.WriteLine("Привет мир!");
}
}
class Program
{
delegate bool IsEqual(int x);
static void Main(string[] args)
{
int[] integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// найдем сумму чисел больше 5
int result1 = Sum(integers, x => x > 5);
Console.WriteLine(result1); // 30
// найдем сумму четных чисел
int result2 = Sum(integers, x => x % 2 == 0);
Console.WriteLine(result2); //20
Console.Read();
}
private static int Sum(int[] numbers, IsEqual func)
{
int result = 0;
foreach (int i in numbers)
{
if (func(i))
result += i;
}
return result;
}
}
int result1 = Sum(integers, x => x > 5);
int result2 = Sum(integers, x => x % 2 == 0);