C # assignment of language Series Lectures (9)

Source: Internet
Author: User
Delegate

Delegate is a new data type introduced by C #. It is very similar to function pointers in C/C ++ and is often used for dynamic method calls that are not bound during compilation. Unlike function pointers, delegates fully implement object-oriented in C #, which can reference both static methods and instance methods, while function pointers can only reference static methods. The delegation in C # is also type-safe.

As an object-oriented data type, delegates can be used in three steps: Delegate declarations, delegate instantiation, and delegate calls. The delegate Declaration defines the data type of a method body (static method or instance method) that encapsulates the specific parameter type and return value type. See the following example:

Delegate int compute (INT left, int right );

As you can see, the delegate type compute contains two elements of the method: parameter type and return value type. Only when the following two conditions are met can the delegate type and method be said to be compatible:

1. The number of parameters is the same, and their types are also in the same order;

2. return values are the same.

The same delegate type indicates that the declared types are the same (same name ). If the delegated instances are equal, the methods they bind are the same method, or the same methods are the same method chain in the same order, and their types only need to be compatible (the above two conditions are also met), without the need to force the same name.

Delegate instantiation is the process of binding a delegate type to a specific method. Similar to the instantiation of other objects, the new statement is required, only method names compatible with the delegate type must be accepted as parameters of the new statement. For the instance method, you must add a number between the two to provide both the Instance Object and method.

After delegation instantiation, you can delegate the call just like calling a method. The following is a complete example, typically showing the three steps of using delegates:

Using system;

Delegate int compute (INT left, int right );

// Delegate type declaration

Class

{

Public int MUL (INT num1, int num2)

{Return num1 * num2;

}

}

Class Test

{

Public static int add (INT num1, int num2)

{Return num1 + num2;

}

Static void main ()

{Compute C1 = new compute (ADD );

// Static method instantiation

A A = new ();

Compute C2 = new compute (A. Mul );

// Instance method instantiation

Int R1 = C1 (2, 3); // delegate call

Int r2 = c2 (2, 3); // delegate call

Console. writeline (R1 );

Console. writeline (R2 );

}

}

Delegate combination

A delegate combination is a method that can be bound to multiple callable methods at the same time. Because multiple methods are bound, C # specifies that the return type of the combination delegate must be void. Of course, the parameter types of these methods must also be compatible with the parameter types of the combination delegate. The delegate combination uses "+" or "+ =" to combine two Delegate types into a new composite delegate type; use "-" or "-=" to remove a delegate instance bound to a method from the composite delegate type or a composite delegate instance bound to multiple methods.

It should be noted that, during the combination and removal of delegates, the types of delegates involved in the operations must be the same-note that "same" is not "equal ". See the following example:

Using system;

Delegate void mydelegate (string S );

Class Test

{

Public static void Hello (string S)

{

Console. writeline ("Hello, {0 }!", S );

}

Public static void goodbye (string S)

{

Console. writeline ("goodbye, {0 }!", S );

}

Public static void main ()

{

Mydelegate a, B, c, d;

A = new mydelegate (Hello );

B = new mydelegate (goodbye );

C = a + B; // a combination of delegates

D = C-A; // remove the delegate

A ("");

B ("B ");

C ("C ");

D ("D ");

}

}

Program output:

Hello,!

Goodbye, B!

Hello, C!

Goodbye, C!

Goodbye, D!

You can see that after the delegate combination, the composite delegate type C is bound with two methods: Hello (string S) and goodbye (string s). You can see this from its output.

Note that the method calls in the combination delegate are ordered. If "c = a + B;" is changed to "C = B + A;" in the preceding example ;", the output sequence changes.

Similarly, the removal of delegates is ordered. It always searches for the delegate instance to be removed from the bound delegate instance at the end. Note that the removal here is based on the delegate instance, instead of in the unit of method. You can modify the example above and then see the result:

Public static void main ()

{

Mydelegate a, B, c, d;

A = new mydelegate (Hello );

B = new mydelegate (goodbye );

C = a + B + A; // delegate combination

B + = B; // a combination of delegates

D = C-B; // remove the delegate

A ("");

B ("B ");

C ("C ");

D ("D ");

}

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.