-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17_Event.cs
58 lines (49 loc) · 1.49 KB
/
Day17_Event.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
public class EventExample: IExample
{
// Day 18: C# Event 2
public class CountdownEventArgs:EventArgs
{
public string name;
public CountdownEventArgs(string name){
this.name = name;
}
}
public class Countdown
{
int internalCounter;
string name;
public delegate void CountDownEventHandler(object sender, CountdownEventArgs e);
public event CountDownEventHandler? CountdownCompleted;
public Countdown(int n, string name){
internalCounter = n;
this.name = name;
}
protected virtual void OnCountdownCompleted(CountdownEventArgs e)
{
if (CountdownCompleted != null)
CountdownCompleted(this, e);
}
public void Decrement()
{
internalCounter = internalCounter - 1;
if (internalCounter == 0)
OnCountdownCompleted(new CountdownEventArgs(name));
}
}
public class Receiver{
public void Tip(object? sender , CountdownEventArgs eventArgs){
Console.WriteLine(eventArgs.name + ": Tip!!!");
}
public void Run(){
Countdown countdown = new Countdown(3, "Example");
countdown.CountdownCompleted += Tip;
countdown.Decrement();
countdown.Decrement();
countdown.Decrement();
}
}
public void Run(){
Receiver r = new Receiver();
r.Run();
}
}