Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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)

public ref class MulticastDelegate abstract : public Delegate

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.

In addition to the methods that delegate types inherit from MulticastDelegate, the common language runtime provides two special methods: BeginInvoke and EndInvoke. For more information about these methods, see Calling Synchronous Methods Asynchronously.

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 namespace System;

// This class contains strings. It has a member method that 
// accepts a multicast delegate as a parameter and calls it. 
ref class HoldsStrings
   // The following line causes the compiler to generate 
   // a new delegate class named CheckAndPrintDelegate that 
   // inherits from System.MulticastDelegate. 
   delegate void CheckAndPrintDelegate( String^ str );


   // An ArrayList that holds strings
   System::Collections::ArrayList^ myStringArray;

      myStringArray = gcnew System::Collections::ArrayList;

   // A method that adds more strings to the Collection 
   void addstring( String^ str )
      myStringArray->Add( str );

   // Iterate through the strings and invoke the method(s) that the delegate points to 
   void PrintAllQualified( CheckAndPrintDelegate^ myDelegate )
      System::Collections::IEnumerator^ myEnum = myStringArray->GetEnumerator();
      while ( myEnum->MoveNext() )
         String^ str = safe_cast<String^>(myEnum->Current);
         myDelegate( str );


//end of class HoldsStrings 
// This class contains a few sample methods 
ref class StringFuncs

   // This method prints a String* that it is passed if the String* starts with a vowel 
   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 
   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 function demonstrates using Delegates, including using the Remove and 
// Combine methods to create and modify delegate combinations. 
int main()
   // Declare the HoldsStrings class and add some strings
   HoldsStrings^ myHoldsStrings = gcnew 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 = gcnew HoldsStrings::CheckAndPrintDelegate( StringFuncs::ConStart );
   HoldsStrings::CheckAndPrintDelegate^ VowStartDel = gcnew HoldsStrings::CheckAndPrintDelegate( StringFuncs::VowelStart );

   // Demonstrate that MulticastDelegates may store only one delegate 

   // 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 {0} delegate(s).", DelegateList->Length );
   DelegateList = VowStartDel->GetInvocationList();
   Console::WriteLine( "ConStartVow contains {0} delegate(s).", DelegateList->Length );

   // Determine whether the delegates are System::Multicast delegates 
   if ( dynamic_cast<System::MulticastDelegate^>(ConStartDel) && dynamic_cast<System::MulticastDelegate^>(VowStartDel) )
      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 = dynamic_cast<HoldsStrings::CheckAndPrintDelegate^>(Delegate::Combine( ConStartDel, VowStartDel ));

   // How many delegates is this delegate holding?
   DelegateList = MultiDel->GetInvocationList();
   Console::WriteLine( "\nMulitDel contains {0} delegates.", DelegateList->Length );

   // 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 = dynamic_cast<HoldsStrings::CheckAndPrintDelegate^>(Delegate::Remove( MultiDel, VowStartDel ));
   MultiDel = dynamic_cast<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 );
} //end of main


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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2015 Microsoft