How to use the delegate (delegates) in C # _c# tutorial

Source: Internet
Author: User
Tags anonymous instance method

1. What is a commission?

In fact, I have been thinking about how to explain the Commission, so that the Commission can be more thorough. To be honest, everyone is commissioned to have different opinions, because the point of view of the problem is different. Personally, it can be understood from the following 2 points:

(1) In terms of data structure, a delegate is a user-defined type like a class.

(2) In terms of design patterns, delegates (classes) provide abstractions of methods (objects).

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

As we know, a delegate is an abstraction of a method that stores the address of a series of methods with the same signature and return back to the type. When a delegate is invoked, all the methods that the delegate contains are executed.

2. Definition of delegate type

A delegate is a type, just as a class is a type. As with classes, delegate types must be declared before they are 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. Method can be either an instance method or a static method.

Del1 = new Mydel (MYINSTOBJ.MYM1);
Del2 = new Mydel (SCLASS.OTHERM2);

(2) using shortcut syntax

The fast key syntax, which consists only of the method descriptor. This can be done because there is an implicit conversion between the method name and its corresponding delegate type.

Del1 = myinstobj.mym1;
Del2 = Sclass.otherm2;

5. Assign a value delegate

Because a 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 reclaimed

6. Combined delegate

Delegates can be grouped by using additional operators. This operation will eventually create 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. The delegate combination copies a copy of the operand.

Mydel del1 = Myobj.mymethod;
Mydel Del2 = Sclass.otherm2;
Mydel del3 = Del1 + Del2;  Group Invocation list


7. Delegate plus minus operation

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

You can also use the-= operator to remove a method for a delegate.

Mydel del = Myobj.mymethod;
del = Sclass.otherm2; Add Method
del-= Myobj.mymethod;//Removal method

8. Delegate invocation

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

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

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

9. Anonymous method

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 does not display the declared return value.

Lambda expressions

Lambda expressions are primarily used to simplify the syntax of anonymous methods. In anonymous methods, the delegate keyword is a bit redundant because the compiler already knows that we assign 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 is read as "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

The above is the entire content of this article, I hope that you learn C # program to help.

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.