C # advanced programming version 6 book note 2 (delegated)

Source: Internet
Author: User

For me, I have always been confused. I have to read books and understand them again soon. Now I will record my understanding in white text.

Good article address: http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html

1. Declare delegation.

A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method. This way, the method is dynamically assigned to the parameter, it can avoid using the IF-else (switch) statement in a large number in the program, and make the program more scalable.

A good way to understand the delegate is to treat the delegate as a name for the method signature and return type. Its syntax definition is similar to the method definition, but there is no method body, the method must match the delegate signature.

Definition

[Access modifier] delegate [return type] deleteagemethod (parameter). For example:

Public Delegate void invoke (INT parameter)

Usage:

In the static main () method, the method is declared as static.

// Delegate method public static void add (int x) {x = x + X; console. writeline ("auto-increment as" + x);} // delegate method public static void multi (INT y) {Y = y * Y; console. writeline ("multiplication is" + Y);}/* commissioned test */invoke voke = new invoke (Multi, you only need to delegate the instance to transfer the address name. This is called deleGATE inference. As long as the compiler can resolve the delegate security to a specific type, this C # feature is valid. for example, // invoke voke = multi; voke (4 );

 

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.