Delegate is like function pointer.
- Delegate used to invokes a function.
- Multicast delegate used to invoke more than one function.
class demo
{
// delegates are used to invoke function
delegate int mathdelegate(int a, int b);
public demo()
{
mathdelegate objDelegate = new mathdelegate(this.add);
int result = objDelegate(10, 20);
Console.WriteLine(result);
//multicast delegates are used to invoke more than one function
objDelegate += new mathdelegate(sub);
result = objDelegate(30, 10);
Console.WriteLine(result);
objDelegate += new mathdelegate(mul);
result = objDelegate(30, 10);
Console.WriteLine(result);
}
public int add(int a, int b)
{
return a + b;
}
public int sub(int a, int b)
{
return a - b;
}
public int mul(int a, int b)
{
return a * b;
}
}
No comments:
Post a Comment