C # Warm up

Source: Internet
Author: User

Drop C # For a long time, just warm up.

Delegate

 

Official definition

A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C ++. unlike C function pointers, delegates are object-oriented, type safe, and secure. the type of a delegate is defined by the name of the delegate. 

 

Delegate types are derived from the delegate class in the. NET Framework. 

 

Along with the staticDelegatemethodShown previusly, we now have three methods that can be wrapped byDelInstance.

A delegate can call more than one method when invoked. this is referred to as multicasting. to add an extra Method to the Delegate's list of methods-the invocation list-simply requires adding two delegates using the addition or addition assignment operators ('+' or '+ = '). for example:

 


Methodclass OBJ = New Methodclass ();
Del d1 = obj. Method1;
Del D2 = obj. method2;
Del D3 = delegatemethod;

//Both types of assignment are valid.
Del allmethodsdelegate = D1 + D2;

 

Allmethodsdelegate

+ = D3;

 

 

At this point Allmethodsdelegate Contains three methods in its invocation list- Method1 , Method2 , And Delegatemethod . The original three delegates, D1 , D2 , And D3 , Remain unchanged. When Allmethodsdelegate Is invoked, all three methods are called in order. if the delegate uses reference parameters, the reference is passed sequentially to each of the three methods in turn, and any changes by one method are visible to the next method. when any of the methods throws an exception that is not caught within the method, that exception is passed to the caller of the Delegate and no subsequent methods in the invocation list are called. if the delegate has a return value and/or out parameters, it returns the return value and parameters of the last method invoked. to remove a method from the invocation list, use the decrement or decrement assignment operator ('-' or '-= ').

Multiple delegates: ordered calls. Each delegate is input for parameters. Modifications to the content are passed to the next delegate. If a return value exists, the return value of the last delegate is returned.

Delegate: keyword used for declaration, in lower case

// Declares a delegate for a method that takes in an int and returns a string.

Public   Delegate String mymethoddelegate ( Int Myint );  


 

 

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.