Wednesday, March 10, 2010

what is multicast delegate?

What is multicast delegate?

Multicast delegate is delegate which holds more than one method reference. When you call delegate, it will execute all the methods which are associated with that delegate.

Sometime we assigned two functions to one delegate it calls multicast delegate. Just take an example, if you have one delegate to transfer fund from one account number to another account. You also want to log every entry into your log system so you can solve any customer query if any.

We have one delegate which is used to transfer funds from one account to another account. Delegate syntax is displayed as below:

delegate void TransferFund(string FromAccountNumber, string ToAccountNumber, decimal Amount);

Now in your class, you can give more than one method reference to this delegate. Code for this is as below:

private void Form1_Load(object sender, EventArgs e)
TransferFund transferFundObject = new TransferFund(DoTransferFund);
transferFundObject += new TransferFund(DoTransferFundLog);
transferFundObject("From_Account_No", "To_Account_No", 1000);


void DoTransferFund(string FromAccountNumber, string ToAccountNumber, decimal Amount)


void DoTransferFundLog(string FromAccountNumber, string ToAccountNumber, decimal Amount)


In above code, In Form load event, I have registered two methods with one delegate. When we call the delegate it will execute both methods one by one.

No comments:

Post a Comment

DotNet Code Guru