-
Notifications
You must be signed in to change notification settings - Fork 37
/
DelegateExample.cs
72 lines (62 loc) · 2.31 KB
/
DelegateExample.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
//____________________________________________________________________________________________________________________________________
//
// Copyright (C) 2024, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community by pressing the `Watch` button and get started commenting using the discussion panel at
//
// https://github.com/mpostol/TP/discussions/182
//
// by introducing yourself and telling us what you do with this community.
//_____________________________________________________________________________________________________________________________________
using System;
namespace TP.FunctionalProgramming
{
public class DelegateExample
{
/// <summary>
/// Delegate PerformCalculation - the declaration of a delegate type
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
public delegate int PerformCalculation(int x, int y);
/// <summary>
/// The perform calculation variable
/// </summary>
/// <returns>System.Int32.</returns>
public PerformCalculation PerformCalculationVar;
/// <summary>
/// An example of event
/// </summary>
public event EventHandler PerformSumMethodCalled;
/// <summary>
/// Initializes a new instance of the <see cref="DelegateExample"/> class.
/// </summary>
public DelegateExample()
{
PerformCalculationVar = new PerformCalculation(PerformSumMethod);
PerformCalculationVar = new PerformCalculation(PerformSubtractMethod);
//PerformCalculationVar = new PerformCalculation(PerformSubtractDoubleMethod); //No overload for 'DelegateExample.PerformSubtractDoubleMethod(double, double)' matches delegate 'DelegateExample.PerformCalculation'
}
public int PerformCalculationMethod(int x, int y)
{
return (PerformCalculationVar?.Invoke(x, y)).GetValueOrDefault();
}
public int PerformSumMethod(int x, int y)
{
PerformSumMethodCalled?.Invoke(this, System.EventArgs.Empty);
checked { return x + y; }
}
public double PerformSumMethod(double x, double y)
{
checked { return x + y; }
}
public static int PerformSubtractMethod(int x, int y)
{
checked { return x - y; }
}
public static double PerformSubtractDoubleMethod(double x, double y)
{
checked { return x - y; }
}
}
}