1. What is a delegation?
In fact, I have been thinking about how to explain the commission so that I can explain the commission more thoroughly. To be honest, everyone has different opinions on the entrusted issue because they look at the problem in different angles. Personally, I think it can be understood from the following two points:
(1) From the perspective of data structure, delegates are a user-defined type just like classes.
(2) From the perspective of design pattern, the delegation (class) provides an abstraction of methods (objects).
Since delegate is a type, what data does it store?
We know that delegates are abstractions of methods, which store addresses 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
Delegates are types, just as if classes are types. Like a class, 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 delete keyword.
(2) Return type + delegate type name + parameter list.
3. Declare delegate variables
MyDel del1,del2;
4. Initialize the delegate variable
(1) Use the new operator
The operands of the new operator are as follows:
Delegate type name
A set of parentheses containing the name of the method that is the first member of the call list. The method can be an instance method or a static method.
del1 = new MyDel( myInstObj.MyM1 ); del2 = new MyDel( SClass.OtherM2 );
(2) Use shortcut syntax
Fast key syntax, it consists of only 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 delegates are reference types, we can change the method address reference contained in the delegate variable by assigning values to it. Old references will be recycled by the garbage collector.
MyDel del; del = myInstaObj.MyM1; //Delegate initializationdel = SClass.OtherM2;//Delegate reassignment,Old references will be recycled
6. Combination Commission
Delegates can be combined using additional operators. This operation will eventually create a new delegate whose call list is a connection to a copy of the delegate call list of two operands.
Delegates are constant and operand delegation will not be changed after creation. The delegate combination copies a copy of the operand.
MyDel del1 = ; MyDel del2 = SClass.OtherM2; MyDel del3 = del1 + del2; //Combination call list
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 the method for delegate.
MyDel del = ; del += SClass.OtherM2; // Add methoddel -= ; // Removal method
8. Delegate call
Delegated calls are similar to method calls. After the delegate call, each method of the call list will be executed.
Before calling the delegation, it should be determined whether the delegation is empty. Calling an empty delegation will throw an exception.
if(null != del) { del();//Delegate call}
9. Anonymous method
Anonymous methods are methods declared inline when initializing a delegate.
Basic structure:
delete(parameter) { statement block }
For example:
delegate int MyDel (int x); //Define a delegate MyDel del = delegate( int x){ return x; };
From the above we can see that the anonymous method will not display the declared return value.
10. Lambda Expressions
Lambda expressions are 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 anonymous methods to Lambda expressions:
Delete the delegate keyword
Anti-Lambda operator => between parameter list and anonymous method body. The Lambda operator is read as "goes to".
MyDel del = delegate( int x) { return x; };//Anonymous methodMyDel del2 = (int x) => {return x;};//Lambda expressionMyDel del3 = x => {return x};//AbbreviationLambdaexpression
The above is all about this article, I hope it will be helpful for everyone to learn C# programming.