40
1
using System;
2
3
//create a static class with static methods
4
public static class MathDelegate
5
{
6
public static double MultipleByTwo(double value)
7
{
8
return value * 2;
9
}
10
11
public static double Square(double value)
12
{
13
14
return value * value;
15
16
}
17
18
19
}
20
21
//declared delegate
22
public delegate double DoubleOp(double x);
23
24
public class DelegateExampleInCsharp
25
{
26
public static void Main()
27
{
28
//call multiple by two
29
DoubleOp operations =MathDelegate.MultipleByTwo;
30
var result = operations(5.2);
31
Console.WriteLine("Multiple By Two="+result);
32
33
//call square the number
34
operations =MathDelegate.Square;
35
result = operations(5.2); //this time value will be multiples by itself
36
Console.WriteLine("Sqaure results="+result);
37
38
}
39
40
}
Cached Result