Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Definition, declaration, instantiation, and invocation of delegates

1. My own definition is easier to understand

1. Pass the method as an argument, but the type is safe.

Declare, instantiate, invoke
1. Delegate with arguments and no return value
Public delegate void DelegateNoReturn(int param); DelegateNoReturn DNR = new DelegateNoReturn(t => {console.writeline (t); });Copy the code
2. Delegate with argument and return value
public delegate int DelegateReturnAndParam(int param); DelegateReturnAndParam drap = new DelegateReturnAndParam(t => {return 1; });Copy the code
3. Delegate with argument and return value
Public delegate int DelegateNoParam(); DelegateNoParam = new DelegateNoParam(() => {return 1; }); Static void Main(string[] args) {new Program().dnr(5); new Program().drap(5); new Program().dnp(); }Copy the code

Generic delegate Action,Func

What is a generic delegate?

A: The end result is A package based on A combination of Delagate and generics.

1: Action for methods with no return value (parameters can be passed as they please) 2: Func for methods with return value (again parameters depend on the case)Copy the code

The nature of entrustment

Delegate is a class
internal delegate void TestDelagate();
Copy the code
1. The compiler first defines a complete class that inherits from the MulticastDelegate class (all delegates derive from MulticastDelegate, and MulticastDelegate derives from Delegate>Object, which for historical reasons consists of two Delegate classes). 2. There are four methods in the class, namely constructor method, Invoke, BeginInvoke and EndInvoke methods. 3. Since delegates are classes, delegates can be defined wherever classes can be defined.Copy the code
The MulticastDelegate contains three non-public fields
1. _target is of type Object, null when the delegate wraps a static method, and reference the Object the callback will operate on when the delegate wraps an instance method (this is the scope of the callback). 2. _MethodPtr is IntPtr, which the CLR uses to identify the method to call back. 3. _invocationList is an Object of type usually null, which references an array of delegates when constructing a delegate chain.Copy the code
3. Each delegate has a constructor
1. Get two arguments, an Object reference and an integer that references the callback method. The Object reference is passed to _target of the Object parameter type. 2. Another callback to IntPtr parameter type _MethodPtr sets _target and _invocationList to null for static methods.Copy the code
4, combined with a clear above the small chestnut
public class delegateTest { public delegate void DelegateNoReturn(int param); / / instantiate a delegate to a static method DelegateNoReturn staticCallMethod = new DelegateNoReturn (delegateTest. StaticMethod); DelegateNoReturn instanceCallMethod = new DelegateNoReturn(new delegateTest().delegecAllMethod); Public static void staticMethod(int Param){} public void instanceMethod(int Param){}Copy the code

5. Use delegate callback to call multiple methods (delegate chain)