32 lines
1.0 KiB
C#
32 lines
1.0 KiB
C#
namespace Delegates;
|
|
|
|
class Program
|
|
{
|
|
public delegate int MathFunction(int a, int b);
|
|
public delegate void PrintFunction(int n);
|
|
static void Main(string[] args)
|
|
{
|
|
int x = 16, y = 2;
|
|
MathFunction mf = MathTool.Sum;
|
|
Console.WriteLine("mf({0}, {1}) = {2}", x, y, mf(x, y));
|
|
mf = MathTool.Diff;
|
|
Console.WriteLine("mf({0}, {1}) = {2}", x, y, mf(x, y));
|
|
PrintFunction pf = MathTool.PrintSquare;
|
|
pf += MathTool.PrintSquareRoot;
|
|
pf(x);
|
|
#pragma warning disable CS8601 // Possible null reference assignment.
|
|
pf -= MathTool.PrintSquare;
|
|
#pragma warning restore CS8601 // Possible null reference assignment.
|
|
Console.WriteLine();
|
|
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
|
pf(y); //instead of pragma pf?.Invoke(y); can be used
|
|
#pragma warning restore CS8602 // Dereference of a possibly null reference.
|
|
|
|
Func<int, int, int> func = MathTool.Sum;
|
|
Console.WriteLine("func({0}, {1}) = {2}", x, y, func(x, y));
|
|
|
|
Action<int> action = MathTool.PrintSquare;
|
|
action(x);
|
|
}
|
|
}
|