-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegates.cs
58 lines (48 loc) · 1.42 KB
/
delegates.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Delegates
{
internal class delegates
{
private delegate void ReceivesInt(int i);
private static event ReceivesInt? RandomEvent;
private static bool IsPrime(int x)
{
if (x < 2)
return false;
else if (x == 2)
return true;
else if (x % 2 == 0)
return false;
return Enumerable.Range(2, (int)Math.Sqrt(x) - 1).All(divisor => x % divisor != 0);
}
private static void PrintIfPrime(int x)
{
if (IsPrime(x))
Console.WriteLine($"{x} is prime");
}
private static void PrintIfDivisableByThree(int x)
{
if (x % 3 == 0)
Console.WriteLine($"{x} is divisable by 3");
}
static void Main(string[] args)
{
RandomEvent += PrintIfPrime;
RandomEvent += PrintIfDivisableByThree;
Random rng = new Random();
Task.Run(async () =>
{
while (true)
{
RandomEvent?.Invoke(rng.Next());
await Task.Delay(1000);
}
});
Console.ReadLine();
}
}
}