Detailed explanation of using C# delegates (Delegates)

黄舟
Release: 2016-12-13 13:34:14
Original
1390 people have browsed it

1. What is delegation?

Actually, I have been thinking about how to explain delegation more thoroughly. To be honest, everyone has different opinions because they look at the problem from different angles. Personally, I think it can be understood from the following two points:

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

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

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

We know that a delegate is an abstraction of a method, and it stores the addresses of a series of methods with the same signature and return type. When a delegate is called, all methods contained in the delegate will be executed.

2. Definition of delegate type

A delegate is a type, just like a class is a type. Like classes, delegate types must be declared before they can be used to create variables and type objects.

delegate void MyDel(int x);
Copy after login
Copy after login

Delegate type declaration:

(1) Begins with delegate keyword.

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

delegate void MyDel(int x);
Copy after login
Copy after login

3. Declare delegate variables

MyDel del1,del2;
Copy after login

4. Initialize delegate variables

(1) Use the new operator

The composition of the operands of the new operator is as follows:

Delegate type name

A set of parentheses, where Contains the name of the method that is the first member of the call list. Methods can be instance methods or static methods.

del1 = new MyDel( myInstObj.MyM1 ); del2 = new MyDel( SClass.OtherM2 );
Copy after login

(2) Use shortcut syntax

quick key syntax, which consists only of method specifiers. This works because there is an implicit conversion between the method name and its corresponding delegate type.

del1 = myInstObj.MyM1; del2 = SClass.OtherM2;
Copy after login

5. Assignment delegate

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

MyDel del; del = myInstaObj.MyM1; //委托初始化del = SClass.OtherM2;//委托重新赋值,旧的引用将被回收
Copy after login

6. Combining Delegates

Delegates can be combined using additional operators. This operation ultimately creates a new delegate whose call list is the concatenation of copies of the delegate call lists of the two operands.

The delegate is constant and the operand delegate will not be changed after it is created. The delegate combination copies a copy of the operand.

MyDel del1 = myObj.MyMethod; MyDel del2 = SClass.OtherM2; MyDel del3 = del1 + del2; //组合调用列表
Copy after login

7. Delegate addition and subtraction operations

You can use the += operator to add new methods to the delegate.

You can also use the -= operator to remove methods from delegates.

MyDel del = myObj.MyMethod; del += SClass.OtherM2; // 增加方法 del -= myObj.MyMethod; // 移除方法
Copy after login

8. Delegate call

Delegate call is similar to method call. After the delegate is called, each method in the call list will be executed.

Before calling the delegate, you should determine whether the delegate is empty. Calling an empty delegate will throw an exception.

if(null != del) { del();//委托调用 }
Copy after login

9. Anonymous methods

Anonymous methods are methods declared inline when initializing the delegate.

Basic structure:

deleage( 参数 ) { 语句块 }
Copy after login

For example:

delegate int MyDel (int x); //定义一个委托 MyDel del = delegate( int x){ return x; };
Copy after login

From the above we can see that anonymous methods do not declare return values.


10. Lambda expression

Lambda表达式主要用来简化匿名方法的语法。在匿名方法中,delegate关键字有点多余,因为编译器已经知道我们将方法赋值给委托。通过几个简单步骤,我们就可以将匿名方法转换为Lambda表达式:

删除delegate关键字

在参数列表和匿名方法主体之间防Lambda运算符=>。Lambda运算符读作"goes to"。

MyDel del = delegate( int x) { return x; };//匿名方法 MyDel del2 = (int x) => {return x;};//Lambda表达式 MyDel del3 = x => {return x};//简写的Lambda表达式
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!