C # Delegate 3 (Multicast delegate)

Source: Internet
Author: User

Sometimes, we want to call a delegate, but we can execute multiple methods at the same time (the most common in custom events), for example, a jobCompositionAfter the file is generated, the system will generate the document log and save it to the database. For the above two operations, if you only want to call one delegate, you can complete it in sequence, you can use multicast delegation.

If you call multicast delegates, you can call multiple methods in sequence. Therefore, the delegate signature must return void (otherwise, where should the return value be sent ?) (When a delegate only contains one method, its return type declaration can refer to the encapsulated method, not necessarily void ). In fact, if the compiler finds that a delegate returns void, it automatically assumes that this is a multicast delegate.

The multicast delegate (multicastdelegate) provides a hook mechanism similar to the pipeline. All the delegates attached to this pipeline are executed sequentially. Because all delegates are inherited from multicastdelegate, the delegates have multicast features.

 

Download Sample Code

The following example shows the multicast delegation:

 
/// Method 1 static void firstmethod (string test) {console. writeline ("method 1 has been completed! ");} /// <Summary> /// method 2 /// </Summary> /// <Param name = "test"> </param> static void secondmethod (string test) {console. writeline ("method 2 has been executed! ");}

The preceding two methods with the same signature are defined and no return value is returned.

 
/// <Summary> /// declare a delegate, used for multicast operations /// </Summary> /// <Param name = "test"> </param> delegate void mymulticastdelegate (string test );

It also declares a delegate with the same signature as the two methods to implement our multicast delegate operation.

 
Static void main (string [] ARGs) {// first create a delegate pointing to the firstmethod method mymulticastdelegate myfirst = new mymulticastdelegate (firstmethod ); /// then create a delegate pointing to the secondmethod method mymulticastdelegate mysecond = new mymulticastdelegate (secondmethod); // merge the two methods into one multicast delegate mymulticastdelegate multicastdelegate = myfirst; // Add the new delegate element multicastdelegate = multicastdelegate + mysecond to the call chain of the multicast delegate; // call the multicast delegate, and execute the multicastdelegate ("test"); console. read ();}

Then, perform the following operations:

 
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.