Export (0) Print
Expand All
3 out of 8 rated this helpful - Rate this topic

MulticastDelegate Class

Represents a multicast delegate; that is, a delegate that can have more than one element in its invocation list.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class MulticastDelegate : Delegate
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class MulticastDelegate extends Delegate
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class MulticastDelegate extends Delegate
Not applicable.

MulticastDelegate is a special class. Compilers and other tools can derive from this class, but you cannot derive from it explicitly. The same is true of the Delegate class.

A MulticastDelegate has a linked list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.

The following example demonstrates the use of a class that is derived from MulticastDelegate.

using System;

    // This class contains strings. It has a member method that
    // accepts a multicast delegate as a parameter and calls it.

    class HoldsStrings
    {
        // The following line causes the compiler to generate
        // a new delegate class named CheckAndPrintDelegate that
        // inherits from System.MulticastDelegate.
        public delegate void CheckAndPrintDelegate(string str);

        // An ArrayList that holds strings
        private System.Collections.ArrayList myStringArray = new System.Collections.ArrayList();

        // A method that adds more strings to the Collection
        public void addstring( string str) {
            myStringArray.Add(str);
        }

        // Iterate through the strings and invoke the method(s) that the delegate points to
        public void PrintAllQualified(CheckAndPrintDelegate myDelegate) {
            foreach (string str in myStringArray) {
                myDelegate(str);
            }
        }
    }   //end of class HoldsStrings

    // This class contains a few sample methods
    class StringFuncs
    {
        // This method prints a string that it is passed if the string starts with a vowel
        public static void ConStart(string str) {
            if (!(str[0]=='a'||str[0]=='e'||str[0]=='i'||str[0]=='o'||str[0]=='u'))
                Console.WriteLine(str);
        }

        // This method prints a string that it is passed if the string starts with a consonant
        public static void VowelStart(string str) {
            if ((str[0]=='a'||str[0]=='e'||str[0]=='i'||str[0]=='o'||str[0]=='u'))
                Console.WriteLine(str);
        }
    }

    // This class demonstrates using Delegates, including using the Remove and
    // Combine methods to create and modify delegate combinations.
    class Test
    {
        static public void Main()
        {
            // Declare the HoldsStrings class and add some strings
            HoldsStrings myHoldsStrings = new HoldsStrings();
            myHoldsStrings.addstring("This");
            myHoldsStrings.addstring("is");
            myHoldsStrings.addstring("a");
            myHoldsStrings.addstring("multicast");
            myHoldsStrings.addstring("delegate");
            myHoldsStrings.addstring("example");

            // Create two delegates individually using different methods
            HoldsStrings.CheckAndPrintDelegate ConStartDel =
                new HoldsStrings.CheckAndPrintDelegate(StringFuncs.ConStart);
            HoldsStrings.CheckAndPrintDelegate VowStartDel =
                new HoldsStrings.CheckAndPrintDelegate(StringFuncs.VowelStart);

            // Demonstrate that MulticastDelegates may store only one delegate
            Delegate [] DelegateList;

            // Returns an array of all delegates stored in the linked list of the
            // MulticastDelegate. In these cases the lists will hold only one (Multicast) delegate
            DelegateList = ConStartDel.GetInvocationList();
            Console.WriteLine("ConStartDel contains " + DelegateList.Length + " delegate(s).");
            DelegateList = VowStartDel.GetInvocationList();
            Console.WriteLine("ConStartVow contains " + DelegateList.Length + " delegate(s).");

            // Determine whether the delegates are System.Multicast delegates
            // if (ConStartDel is System.MulticastDelegate && VowStartDel is System.MulticastDelegate) {
                Console.WriteLine("ConStartDel and ConStartVow are System.MulticastDelegates");
            // }

            // Run the two single delegates one after the other
            Console.WriteLine("Running ConStartDel delegate:");
            myHoldsStrings.PrintAllQualified(ConStartDel);
            Console.WriteLine("Running VowStartDel delegate:");
            myHoldsStrings.PrintAllQualified(VowStartDel);

            // Create a new, empty MulticastDelegate
            HoldsStrings.CheckAndPrintDelegate MultiDel;

            // Delegate.Combine receives an unspecified number of MulticastDelegates as parameters
            MultiDel = (HoldsStrings.CheckAndPrintDelegate) Delegate.Combine(ConStartDel, VowStartDel);

            // How many delegates is this delegate holding?
            DelegateList = MultiDel.GetInvocationList();
            Console.WriteLine("\nMulitDel contains " + DelegateList.Length + " delegates.");

            // What happens when this mulitcast delegate is passed to PrintAllQualified
            Console.WriteLine("Running the multiple delegate that combined the first two");
            myHoldsStrings.PrintAllQualified(MultiDel);

            // The Remove and Combine methods modify the multiple delegate
            MultiDel = (HoldsStrings.CheckAndPrintDelegate) Delegate.Remove(MultiDel, VowStartDel);
            MultiDel = (HoldsStrings.CheckAndPrintDelegate) Delegate.Combine(MultiDel, ConStartDel);

            // Finally, pass the combined delegates to PrintAllQualified again
            Console.WriteLine("\nRunning the multiple delegate that contains two copies of ConStartDel:");
            myHoldsStrings.PrintAllQualified(MultiDel);

            return;
        }   //end of main
    }   //end of Test

import System.*;

// This class contains strings. It has a member method that
// accepts a multicast delegate as a parameter and calls it.

class HoldsStrings
{
    /** @delegate
     */

    // This delegate is declared as a void, so the compiler automatically
    // generates a new class, named CheckAndPrintDelegate, that inherits from
    // System.MulticastDelegate.
    public delegate void CheckAndPrintDelegate(String str);

    // An ArrayList that holds strings
    private System.Collections.ArrayList myStringArray =
        new System.Collections.ArrayList();

    // A method that adds more strings to the Collection
    public void addstring(String str)
    {
        myStringArray.Add(str);
    } //addstring

    // Iterate through the strings and invoke the method(s) that the delegate
    // points to
    public void PrintAllQualified(CheckAndPrintDelegate myDelegate)
    {
        for (int iCtr1 = 0; iCtr1 < myStringArray.get_Count(); iCtr1++) {
            String str = System.Convert.ToString(myStringArray.get_Item(iCtr1));
            myDelegate.Invoke(str);
        }
    } //PrintAllQualified
} //HoldsStrings
//end of class HoldsStrings

// This class contains a few sample methods
class StringFuncs
{
    // This method prints a string that it is passed if the string starts 
    // with a vowel
    public static void ConStart(String str)
    {
        if (!(str.get_Chars(0) == 'a' || str.get_Chars(0) == 'e'
            || str.get_Chars(0) == 'i' || str.get_Chars(0) == 'o'
            || str.get_Chars(0) == 'u')) {
            Console.WriteLine(str);
        }
    } //ConStart

    // This method prints a string that it is passed if the string starts with
    // a consonant
    public static void VowelStart(String str)
    {
        if (str.get_Chars(0) == 'a' || str.get_Chars(0) == 'e'
            || str.get_Chars(0) == 'i' || str.get_Chars(0) == 'o'
            || str.get_Chars(0) == 'u') {
            Console.WriteLine(str);
        }
    } //VowelStart
} //StringFuncs

// This class demonstrates using Delegates, including using the Remove and
// Combine methods to create and modify delegate combinations.
class Test
{
    public static void main(String[] args)
    {
        // Declare the HoldsStrings class and add some strings
        HoldsStrings myHoldsStrings = new HoldsStrings();
        myHoldsStrings.addstring("This");
        myHoldsStrings.addstring("is");
        myHoldsStrings.addstring("a");
        myHoldsStrings.addstring("multicast");
        myHoldsStrings.addstring("delegate");
        myHoldsStrings.addstring("example");
        // Create two delegates individually using different methods
        HoldsStrings.CheckAndPrintDelegate ConStartDel = 
            new HoldsStrings.CheckAndPrintDelegate(StringFuncs.ConStart);
        HoldsStrings.CheckAndPrintDelegate VowStartDel = 
            new HoldsStrings.CheckAndPrintDelegate(StringFuncs.VowelStart);
        // Demonstrate that MulticastDelegates may store only one delegate
        Delegate DelegateList[];
        // Returns an array of all delegates stored in the linked list of the
        // MulticastDelegate. In these cases the lists will hold only one 
        // (Multicast) delegate
        DelegateList = ConStartDel.GetInvocationList();
        Console.WriteLine("ConStartDel contains " + DelegateList.get_Length() 
            + " delegate(s).");
        DelegateList = VowStartDel.GetInvocationList();
        Console.WriteLine("ConStartVow contains " + DelegateList.get_Length() 
            + " delegate(s).");
        // Determine whether the delegates are System.Multicast delegates
        // if (ConStartDel is System.MulticastDelegate && VowStartDel is 
        // System.MulticastDelegate) {
        Console.WriteLine("ConStartDel and ConStartVow are " 
            + "System.MulticastDelegates");
        // }
        // Run the two single delegates one after the other
        Console.WriteLine("Running ConStartDel delegate:");
        myHoldsStrings.PrintAllQualified(ConStartDel);
        Console.WriteLine("Running VowStartDel delegate:");
        myHoldsStrings.PrintAllQualified(VowStartDel);
        // Create a new, empty MulticastDelegate
        HoldsStrings.CheckAndPrintDelegate multiDel;
        // Delegate.Combine receives an unspecified number of MulticastDelegates
        // as parameters
        multiDel = (HoldsStrings.CheckAndPrintDelegate)(Delegate.Combine(
            ConStartDel, VowStartDel));
        // How many delegates is this delegate holding?
        DelegateList = multiDel.GetInvocationList();
        Console.WriteLine("\nMulitDel contains " + DelegateList.get_Length() 
            + " delegates.");
        // What happens when this mulitcast delegate is passed to 
        // PrintAllQualified
        Console.WriteLine("Running the multiple delegate that combined the "
            + "first two");
        myHoldsStrings.PrintAllQualified(multiDel);
        // The Remove and Combine methods modify the multiple delegate
        multiDel = (HoldsStrings.CheckAndPrintDelegate)(Delegate.Remove(
            multiDel, VowStartDel));
        multiDel = (HoldsStrings.CheckAndPrintDelegate)(Delegate.Combine(
            multiDel, ConStartDel));
        // Finally, pass the combined delegates to PrintAllQualified again
        Console.WriteLine("\nRunning the multiple delegate that contains two " 
            + "copies of ConStartDel:");
        myHoldsStrings.PrintAllQualified(multiDel);
        return;
    } //main 
} //Test 

System.Object
   System.Delegate
    System.MulticastDelegate
       Derived Classes
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.