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);
Delegate type declaration:
(1) Begins with delegate keyword.
(2) Return type + delegate type name + parameter list.
3. Declare the delegate variable
MyDel del1,del2;
4. Initialize the delegate variable
(1) Use the new operator
The composition of the operands of the new operator is as follows:
Delegation type name
A set of circles Parentheses containing 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 );
(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;
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;//委托重新赋值,旧的引用将被回收
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; //组合调用列表
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; // 移除方法
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();//委托调用}
9. Anonymous methods
Anonymous methods are methods declared inline when initializing the delegate.
Basic structure:
deleage( 参数 ) { 语句块 }
For example:
delegate int MyDel (int x); //定义一个委托 MyDel del = delegate( int x){ return x; };
From the above we can see that anonymous methods will not display the declared return value.
10. Lambda expression
Lambda expression is mainly 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 into a Lambda expression:
Remove the delegate keyword
Prevent the Lambda operator => between the parameter list and the anonymous method body. The lambda operator is pronounced "goes to".
MyDel del = delegate( int x) { return x; };//匿名方法 MyDel del2 = (int x) => {return x;};//Lambda表达式 MyDel del3 = x => {return x};//简写的Lambda表达式