C # Delegate usage details (Delegates)

Source: Internet
Author: User

1. What is a delegate?

In fact, I have been thinking about how to explain the Commission in order to make the delegation more thorough. To tell the truth, everyone has a different opinion because of the angle of the problem. Personally, it can be understood from the following 2 points:

(1) From the data structure, the delegate is the same as the class is a user-defined type.

(2) In terms of design patterns, a delegate (class) provides an abstraction of a method (object).

Since the delegate is a type, what data does it store?

We know that a delegate is an abstraction of a method that stores the address of a series of methods that have the same signature and return type. When the delegate is invoked, all the methods contained by the delegate are executed.

2. Definition of a delegate type

A delegate is a type, as if the class were a type. As with classes, a delegate type must be declared before it is used to create variables and type objects.

delegate void Mydel (int x);

Delegate type declaration:

(1) Start with the Deleagate keyword.

(2) return type + delegate type name + parameter list.

3. Declaring a delegate variable

Mydel Del1,del2;

4. Initialize the delegate variable

(1) using the new operator

The operands of the new operator are composed of the following:

Delegate type name

A set of parentheses that contains the name of the method that is the first member in the invocation list. The method can be an instance method or a static method.

Del1 = new Mydel (myinstobj.mym1);d El2 = new Mydel (SCLASS.OTHERM2);

(2) using the shortcut syntax

The fast key syntax, which is composed only of the method specifiers. This is possible because there is an implicit conversion between the method name and its corresponding delegate type.

Del1 = Myinstobj.mym1;del2 = Sclass.otherm2;

5. Assignment delegation

Since the delegate is a reference type, we can change the method address reference contained in the delegate variable by assigning it a value. Old references are reclaimed by the garbage collector.

Mydel Del;del = myinstaobj.mym1; Delegate initialization del = sclass.otherm2;//delegate re-assignment, old reference will be recycled

6. Combination of Delegates

Delegates can be combined by using additional operators. This operation eventually creates a new delegate whose invocation list is a connection to a copy of the delegate invocation list of two operands.

The delegate is constant, and the operand is not changed after the delegate is created. A copy of the operand is copied by the delegate combination.

Mydel del1 = Myobj.mymethod; Mydel Del2 = Sclass.otherm2; Mydel del3 = Del1 + Del2;   Combination invocation List

7. Delegate Plus and minus operations

You can use the + = operator to add a method to a delegate.

You can also remove a method from a delegate by using the-= operator.

Mydel del = Myobj.mymethod;del + = Sclass.otherm2; Add Method del-= Myobj.mymethod; Remove method

8. Delegate invocation

A delegate invocation is similar to a method call. After the delegate is called, each method of the invocation list is executed.

Before invoking a delegate, you should determine whether the delegate is empty. Calling an empty delegate throws an exception.

if (null! = del) {     del ();//Delegate Invocation}

9. Anonymous Methods

An anonymous method is a method that is declared inline when the delegate is initialized.

Basic structure:

Deleage (parameter) {statement block}

For example:

delegate int Mydel (int x); Defines a delegate Mydel del = delegate (int x) {return x;};

From the above we can see that the anonymous method is not to display the declared return value.

Ten. Lambda expressions

Lambda expressions are primarily used to simplify the syntax of anonymous methods. In an anonymous method, the delegate keyword is a bit redundant because the compiler already knows that we are assigning the method to the delegate. With a few simple steps, we can convert an anonymous method to a lambda expression:

Delete delegate keyword

Anti-LAMBDA operator = = between the argument list and the anonymous method body. The lambda operator reads "goes to".

Mydel del = delegate (int x) {return x;};//anonymous method Mydel del2 = (int x) + = {return x;};/ /lambda expression Mydel del3 = x = {return x};//shorthand lambda expression
  • 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.