About delegation (2)

Source: Internet
Author: User

The delegate used previously only contains one method call. The number of calls to the delegate is the same as the number of calls to the method. If you want to call multiple methods, you need to call the delegate multiple times. In fact, a delegate can also contain multiple methods. This delegate is a multicast delegate. If multiple methods are called, multiple methods can be called sequentially.

Multicast delegation is derived from system. multicastdelegate. Its combine method allows multiple call methods to be linked together. We can add call methods to the delegate through ++ and ++ =, you can also use-and-= to delete the calling method. For example:

Class Program
{
Private delegate void doubleop (Double X );
Static void main (string [] ARGs)
{
Doubleop operations = mathsopration. mutiplybytwo;
Operations + = mathsopration. Square;
Processanddisplay (operations, 2.0 );
Processanddisplay (operations, 7.9 );
Processanddisplay (operations, 1.414 );

Console. readkey ();
}
Static void processanddisplay (doubleop action, double value)
{
Console. writeline ();
Console. writeline ("processanddisplaynumber called with value = {0}", value );
Action (value );

}

}
Class mathsopration
{
Public static void mutiplybytwo (double value)
{
Double result = value * 2;
Console. writeline ("mutiplying by 2: {0} gives {1}", value, result );
}
Public static void square (double value)
{
Double result = value * value;
Console. writeline ("squaring: {0} gives {1}", value, result );
}
}

 

Result:

 

Note:

1. The void must be returned for the delegate signature in the multicast delegate. Otherwise, only the result of the last method called by the delegate can be obtained.

2. If you use multicast delegates, you should note that the order of the same delegate call chain is not officially defined. Therefore, you should avoid coding that relies on calling methods in a specific order;

3. multicast delegates include a set of delegates called one by one. If an exception is thrown for a method called by Delegate, the entire iteration stops.

Solution: List of manual Iteration Methods

The source code is as follows:

// The code for terminating iteration is displayed with an exception

Public delegate void DemoTrying ();
Class Program
{
Static void Main ()
{
DemoTrying d = One;
D + = Two;
Try
{
D ();
}
Catch (exception E)
{
Console. writeline (E. Message );
}
Console. readkey ();

}
Static void one ()
{
Console. writeline ("one ");
Throw new exception ("error in one ");
}
Static void Two ()
{
Console. WriteLine ("two ");
}
}

 

 

// The changed code

Public delegate void DemoTrying ();
Class Program
{
Static void Main ()
{
DemoTrying d = One;
D + = Two;
Delegate [] delegates = D. getinvocationlist ();
Foreach (demotrying D1 in delegates)
{
Try
{
D1 ();
}
Catch (exception E)
{
Console. WriteLine (e. Message );
}
}

Console. ReadKey ();

}
Static void One ()
{
Console. WriteLine ("one ");
Throw new Exception ("Error in one ");
}
Static void two ()
{
Console. writeline ("two ");
}
}

 

 

Delegated knowledge is very important and extensive. I would like to take a look at other aspects of delegation. If you are interested, you can refer to anonymous methods, lambda expressions, returned type associations, and parameter type resistance changes. Among them, the anonymous method and Lambda expression help to simplify the amount of delegated code (People-oriented)

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.