Delegate can invoke only one method reference has been encapsulated into the delegate.it is possible for certain delegate to hold and invoke multiple methods such delegate called multicast delegates.multicast delegates also know as combinable delegates, must satisfy the following conditions:
- The return type of the delegate must be void. None of the parameters of the delegate type can be delegate type can be declared as output parameters using out keywords.
- Multicast delegate instance that created by combining two delegates, the invocation list is formed by concatenating the invocation list of two operand of the addition operation. Delegates are invoked in the order they are added.
Implement Multicast Delegates Example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- delegate void MDelegate();
- class DM {
- static public void Display() {
- Console.WriteLine("Meerut");
- }
- static public void print() {
- Console.WriteLine("Roorkee");
- }
- }
- class MTest {
- public static void Main() {
- MDelegate m1 = new MDelegate(DM.Display);
- MDelegate m2 = new MDelegate(DM.print);
- MDelegate m3 = m1 + m2;
- MDelegate m4 = m2 + m1;
- MDelegate m5 = m3 - m2;
- m3();
- m4();
- m5();
- }
- }
No comments:
Post a Comment