Delegate in. NET

Source: Internet
Author: User

1.1.1 Definition

A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. The use of delegate methods can have parameters and return values like any other method, as shown in the following example:

// Code in C #

Public delegate int implements mcalculation (int x, int y );

Any method that matches the delegate signature (composed of the return type and parameters) can be assigned to the delegate.

A simple understanding of the Delegate (or proxy) is a data type: Its variables can be referenced in a method that meets the requirements, and can be indirectly called through the Delegate.

Actually.. NET Commission is similar to the function pointer in C language, the difference is. NET delegation is type-safe. This shows that the function pointer in C is just a pointer to a storage unit, and we cannot tell what the pointer actually points.

1.1.2 delegated use
  • The following section describes how to use the delegate:
  • Define a delegate type
  • Call a method for delegated execution
  • Define a delegated instance
  • Call of delegated instance

First, we define a delegate type as follows:

//Custom delegate type

Public Delegate VoidStringProcessor (StringInput );

Then we define the candidate delegation method in step 5 as follows:

VoidPrintString (StringX)

VoidPrintInteger (IntX)

VoidPrintTwoStrings (StringX,StringY)

IntGetStringLength (StringX)

VoidPrintObject (ObjectX)

You can guess which one matches the signature of the delegate type provided above (Signature match: parameter type, number of parameters and return type match ). The answer will be published immediately when it comes to excitement. The method for matching the delegate type is PrintString and PrintObject. If you do not understand it, consider the condition of the delegate match-signature match.

Figure 1 successful delegation output


Now we have a certain understanding of delegation. Next we will introduce the most frequently used part of delegation-events.

We will discuss events from the perspective of the transmitter and receiver. For example, in UI programming, the sender is when you click the mouse or press the keyboard. net clr. Note that the event Sender does not know who the receiver is. This complies with the object-oriented principle, and an event receiver has a method to handle the event. At this time, it must be delegated, as mentioned above, the event sender knows nothing about the event receiver. By entrusting it as an intermediary, the receiver registers the event handling method into the event, in this way, the receiver-> delegate-> receiver process is implemented.

We can think like this: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 a large number of use in the programIf-Else (Switch)Statement, and make the program more scalable.


1.1.3 custom Delegation

It is a bit difficult to understand the above. Next we will analyze what is delegation and how to implement delegation through specific examples. Aren't you very fond of multi-language development? Let's see how our program can speak multiple languages!

        /// <Summary>
///The Englishes speaker.
/// </Summary>
/// <Param name = "name">The name.</Param>
Public VoidEnglishSpeaker (StringName)
Console. WriteLine (
String. Format ("Hello my name is {0} and I am English speaker.", Name ));

/// <Summary>
///The Chineses speaker.
/// </Summary>
Public VoidChineseSpeaker ()
Console. WriteLine (
String. Format ("Hello, my name is {0}. I speak Mandarin.", Name ));

Now we have two ways to speak Mandarin and English. Now our program can speak Mandarin and English. Now, we want to know when to speak Mandarin and when to speak English. It's okay if we just add a judgment. Yes, we can achieve it through switch or if else.

        /// <Summary>
///Call different methods based on the context
/// </Summary>
/// <Param name = "name">String</Param>

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: 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.