Detailed Explanation of the Use of Delegation (Delegates) in C

  • 2021-10-16 02:30:02
  • OfStack

1. What is a commission?

In fact, I think about how to explain the entrustment, so that I can speak the entrustment more thoroughly. To tell the truth, everyone has different opinions because they look at problems from different angles. Personally, it can be understood from the following two points:

(1) In terms of data structure, delegate and class 1 are user-defined types.

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

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

As we know, a delegate is an abstraction of a method that 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

Delegate is a type, just as class is type 1. Like Class 1, 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. Declare delegate variables

MyDel del1,del2;

4. Initialize delegate variables

(1) Use the new operator

The operands of the new operator consist of the following:

Delegate type name
1 set of parentheses containing the name of the method that is the first member in 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 descriptors. 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. Assign delegates

Because 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 collected by the garbage collector.


MyDel del;
del = myInstaObj.MyM1; // Delegate initialization 
del = SClass.OtherM2;// Delegate is re-assigned, and the old reference will be recycled 

6. Combination entrustment

Delegates can be combined with additional operators. This operation eventually creates a new delegate whose invocation list is a connection of copies of the delegate invocation list of two operands.

Delegates are constant, and operand delegates will not be changed after they are created. Delegating a combined copy is a copy of the operand.


MyDel del1 = myObj.MyMethod;
MyDel del2 = SClass.OtherM2;
MyDel del3 = del1 + del2;  // Combined call list 


7. Delegate addition and subtraction

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

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


MyDel del = myObj.MyMethod;
del += SClass.OtherM2; //  Incremental method 
del -= myObj.MyMethod; //  Removal method 

8. Delegate invocation

Delegate calls are similar to method calls. After the delegate is called, each method of the call list will be 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 invocation 
}

9. Anonymous method

Anonymous methods are methods declared inline when a delegate is initialized.

Basic structure:

deleage (parameter) {statement block}
For example:


delegate int MyDel (int x); // Definition 1 Delegation  

MyDel del = delegate( int x){ return x; };

As we can see from the above, anonymous methods will not display the declared return value.

10. Lambda expression

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 methods to delegates. In a few simple steps, we can convert anonymous methods into Lambda expressions:

Delete delegate keyword
Prevent Lambda operator = between parameter list and anonymous method body > . The Lambda operator is read "goes to".


MyDel del = delegate( int x) { return x; };// Anonymous method 
MyDel del2 = (int x) => {return x;};//Lambda Expression 
MyDel del3 = x => {return x};// Abbreviated Lambda Expression 

The above is the whole content of this article, hoping to help everyone learn C # programming.


Related articles: