Please take a look on the below example code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestHome
{
class Program
{
public delegate int AddMulDel(int no1,int no2);
public int Add(int num1st,int num2nd)
{
return num1st+num2nd;
}
public int Mul(int num1st, int num2nd)
{
return num1st * num2nd;
}
static void Main (string[] args)
{
Program p = new Program();
AddMulDel objamDEL = p.Add;
Console.WriteLine(objamDEL(10, 20)); // Result = 30
objamDEL += p.Mul;
Console.WriteLine(objamDEL.Invoke(10, 20)); // Result = 200
//Though you have two methods[Add and Mul] in Delegate reference 'objamDEL',
//Mul method is getting ovirwritten over Add method
foreach (AddMulDel dm in objamDEL.GetInvocationList())
{
List<int> lstInt = new List<int>();
lstInt.Add(dm(10, 20));
foreach (int item in lstInt)
Console.WriteLine(item); //Result :- 30 and 200
}
Console.Read();
}
}
}
.
No comments:
Post a Comment