Beginner's note (C # Delegate)

Source: Internet
Author: User

1. Delegation Overview

In short, the delegate is to pass the method as a parameter.

A delegate is a type of dynamic calling method. It is referenced in the same way as classes, interfaces, and arrays. In C #, you can declare the delegate type, create a delegate instance, and encapsulate methods in the delegate object. A complete set of methods with method names, return values, and parameter lists is provided. The delegation used to reference methods must also have the same return values and parameter lists.

The nature of the delegate object represents the reference (memory address) of the method, which has the following features:

  • Similar to pointers in C ++, however, delegation is completely object-oriented and secure data type.
  • The delegate allows passing methods as parameters.
  • Delegate available callback functions.
  • The delegate can connect multiple methods together. Multiple event handlers can be started when an event is triggered.

2. Declaration, instantiation and use of delegation

2.1 delegated statement

A delegate is a type of referenced data and uses keywords in C #.DelegateStatement, the general form is as follows:

[Access modifier] delegate name of delegate return value type ([parameter list])

2.2 delegation instantiation

A delegate is a special data type. Therefore, it must be instantiated before it can be used to call a method. The general format is as follows:

Delegate type delegate variable name = new delegate Type constructor (delegate method to be referenced)

2.3 use Delegation

After instantiation, you can call the method referenced by the delegate object. When you use a delegate object to call all methods, you must ensure that the parameter type, number, sequence, and method declaration match.

2.4 use the anonymous method

The code block can be passed as a parameter to avoid defining the method separately. The normal form of creating a delegate object using the anonymous method is as follows:

Delegate type delegate variable name = delegate ([parameter list]) {// code block}

3. Multi-channel broadcast and delegated combination

In fact, C # allows a single delegate object to call multiple methods. When more references direct to other methods are added to the delegate, these references are stored in the delegate call list, this type of delegation is called multi-channel broadcast delegation. All C # delegates are implicit multicast delegates. To add multiple method references to the call list of a delegate, you can call all methods at a time through the delegate. This process is called multicast.

There are two ways to implement Multicast:

  • The "+" operator is used to directly combine two delegate objects of the same type.

For example:

Assume that there is a Commission calculate

Calculate a = new calculate (ADD );

Calculate B = new calculate (mulipty );

A = A + B;

Since only one value can be returned for a delegate object and only the return value of the last method in the call list is returned, to avoid confusion, we recommend that you define each method using void when using multi-channel broadcast.

  • Use the "+ =" operator to add the newly created delegate object to the delegate call list Zhong. You can also use the "-=" operator to remove the delegate object from the call list.

 

Beginner's note (C # Delegate)

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.