Introduction
Instantiating delegates are type has been
declare, a delegate object must be create with the new keyword and
instantiation can be use to reference methods that take an integer
parameter and returns an integer value. Show in the example.
Example
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
delegate
int
result(int
n); //create delegate result
namespace
Instantiating_delegate_in_c_sharp
{
class
TestDelegate
//create class TestDelegate
{
static
int number = 5;
public
static
int Sum(int
l) //create static member
function
{
number += l;
return number;
}
public
static
int Mul(int
m)
{
number *= m;
return number;
}
public
static
int display()
{
return number;
}
static
void Main(string[]
args)
{
result r1 =
new
result(Sum);
//create delegate instances
result r2 =
new
result(Mul);
r1(10);
//calling the methods using the
delegate objects
Console.WriteLine("Value
of Num: {0}", display());
r2(5);
Console.WriteLine("Value
of Num: {0}", display());
}
}
}
Output: