-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_31.cs
102 lines (83 loc) · 2.25 KB
/
lesson_31.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
class Counter
{
public int Value { get; set; }
}
Counter c1 = new Counter { Value = 23 };
Counter c2 = new Counter { Value = 45 };
bool result = c1 > c2;
Counter c3 = c1 + c2;
public static возвращаемый_тип operator оператор(параметры)
{ }
class Counter
{
public int Value { get; set; }
public static Counter operator +(Counter c1, Counter c2)
{
return new Counter { Value = c1.Value + c2.Value };
}
public static bool operator >(Counter c1, Counter c2)
{
return c1.Value > c2.Value;
}
public static bool operator <(Counter c1, Counter c2)
{
return c1.Value < c2.Value;
}
}
public static Counter operator +(Counter c1, Counter c2)
{
return new Counter { Value = c1.Value + c2.Value };
}
static void Main(string[] args)
{
Counter c1 = new Counter { Value = 23 };
Counter c2 = new Counter { Value = 45 };
bool result = c1 > c2;
Console.WriteLine(result); // false
Counter c3 = c1 + c2;
Console.WriteLine(c3.Value); // 23 + 45 = 68
Console.ReadKey();
}
public static int operator +(Counter c1, int val)
{
return c1.Value + val;
}
Counter c1 = new Counter { Value = 23 };
int d = c1 + 27; // 50
Console.WriteLine(d);
public static Counter operator ++(Counter c1)
{
c1.Value += 10;
return c1;
}
public static Counter operator ++(Counter c1)
{
return new Counter { Value = c1.Value + 10 };
}
Counter counter = new Counter() { Value = 10 };
Console.WriteLine($"{counter.Value}"); // 10
Console.WriteLine($"{(++counter).Value}"); // 20
Console.WriteLine($"{counter.Value}"); // 20
Counter counter = new Counter() { Value = 10 };
Console.WriteLine($"{counter.Value}"); // 10
Console.WriteLine($"{(counter++).Value}"); // 10
Console.WriteLine($"{counter.Value}"); // 20
Консольный вывод:
class Counter
{
public int Value { get; set; }
public static bool operator true(Counter c1)
{
return c1.Value != 0;
}
public static bool operator false(Counter c1)
{
return c1.Value == 0;
}
// остальное содержимое класса
}
Counter counter = new Counter() { Value = 0 };
if (counter)
Console.WriteLine(true);
else
Console.WriteLine(false);