C # commissioned entry-level learning

Source: Internet
Author: User

Today, I learned how to delegate this chapter on msdn. Although I have seen some webcast videos about the entrusting party and some related introductions on the internet, I have never figured out what the entrusting is. Today, it took me a day to finally unlock her secret.

Delegate. The English name is delegate. You can translate Chinese characters into representatives and act as proxies. As the name suggests, the delegate and the method assigned to her have the same behavior. A delegate is a type of reference method, so it is a type of reference type. Unlike other reference types, delegation references methods. It replaces the function pointer of C ++. In C #, a method is encapsulated in a class, so it is object-oriented and type-safe.

To use delegation, follow these three steps:

1. Declare Delegation

The following statement declares a new delegate type

Public Delegate int implements mcalculation (int x, int y)

The access modifier of this delegate type is public. Why should it be set to public? To create a delegate, you want to call the method encapsulated by the delegate in the external class. Therefore, the access modifier is generally public. the access modifier of the attribute corresponding to the private field is generally set to public. delegate is the keyword used to create the delegate type, which is defined in the C # syntax. Int character mcalculation (int x, int Y) is collectively referred to as the delegate signature. Int Is the return type of the delegate type. performcalculation is the delegate name. because the delegate name determines the type of the Delegate, the delegate type is created based on the delegate name. In this example, a performcalculation delegate type is created. Of course, the name of the delegate name must be meaningful in order to facilitate future calls to it. (int x, int Y) is a list of parameters of this delegate type, including the parameter type, parameter name, and number of parameters. from the declaration of the Delegate, the biggest difference between the delegate and other reference types such as class is the addition of one return type and parameter list. The return type and parameter list form the signature of the delegate. Note that, unlike in the context of method overloading, the method signature does not have a return type. In the context of the Delegate, the delegate signature has a return type. The return type and parameter list of methods encapsulated for the delegate must match the delegate signature, but they may not completely match, for example, if the return type of the delegate encapsulation method is the derived type of the delegate return type, the parameter name of the method can be different from that of the delegate signature, but the parameter type must be the same.

Declaring a delegate only creates a delegate type. To use it, we must instantiate it. Think about it seriously. This is the same as creating a class. We must create an instance of the class before we can create instance members in the category.

2. instantiate the delegate

After declaring the delegate type, you must create a delegate object and associate it with a specific method.

The following statement instantiates the delegate mcalculation created in Step 1:

There are three methods to instantiate a delegate:

1). Use the naming method for instantiation

The delegate can be associated with the naming method. When a delegate is instantiated using the naming method, this method will be passed as a parameter

Int calculate (int x, int y ){/*...... */}

Export mcalculation Pc = new export mcalculation (obj. Calculate)

Or export mcalculation Pc = obj. Calculate

2). Use the anonymous method for instantiation

When you do not want to pay the system overhead for creating a new method, C # enables you to instantiate the delegate and immediately specifyCodeBlock. These are called anonymous methods

Export mcalculation Pc = delegate (int m, int N ){/*...... */}

3). Use the lambra expression

A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression directory tree.

Export mcalculation Pc = x-> X * y

3. Call the delegate

The delegate object is called by the name of the delegate object (followed by the parameter to be passed to the delegate, which is enclosed in brackets ).

PC (5, 6)

Because no application delegate has been developed and lack of relevant experience, there are still many unclear aspects about the delegated application. We look forward to exploring it in future work and study!

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.