What is delegate
Delegate is a reference type variable that contains a reference to a method, and the reference can be changed at runtime. If it is difficult to understand, you can understand the delegate as a function pointer, but there are differences.
Delegate and function pointers in C++:
Function pointers can only point to static functions, while delegate can refer to both static functions and non-static member functions. When referencing non-static member functions, delegate not only saves a reference to the entry pointer of this function, but also saves a reference to the class instance that calls this function.
Secondly, compared with function pointers, delegate is an object-oriented, type-safe, and reliable managed object. In other words, runtime can ensure that delegate points to an effective method, and you don't have to worry that delegate points to an invalid address or an out-of-bound address.
Instantiated delegate: Once the delegate type is declared, the delegate type must be created through the new keyword (equivalent to instantiating an object in an object-oriented manner). When creating a delegate, the parameters passed to the new statement are written like a method call, but without parameters.
// Declaration of delegationpublic delegate void PrintSting(string s); //Instantiated delegationPrintSting ps=new PrintString(WriteToScreen);
Anonymous method:
After the delegation is defined (declared), we have to define the methods that the delegation needs to be used separately. For example, if you define a calculator delegate, you also need to rewrite the calculation method of addition, subtraction, multiplication and division for the calculator delegate to use. At this time we thought of anonymity.
When using anonymous methods, there is no need to write methods such as addition, subtraction, multiplication and division separately. You just need to implement these logic in the body of the anonymous method. Examples are as follows:
delegate int calculator(int x, int y); //Delegate Typestatic void Main(string[] args) { //Create a delegate object (determine which methods to bind to), delegate instance name = new delegate name (a method of a certain class, this example is bound to the addition direction calculator Adding =delegate( int x, int y){ return x+y; }; calculator Moveing = delegate(int x, int y){ return x - y; }; calculator Multiply = delegate(int x, int y) { return x * y; }; calculator Divide = delegate(int x, int y) { return x / y; }; Adding(4, 4);//8 Moveing(4, 4);//0 Multiply(4, 4);//16 Divide(4, 4);//1 }
What is event
We can simply divide event programming into two parts: the class where the event occurs (publisher: publisher) and the class where the event receives and processes (subscriber: subscriber). Events use the publisher-subscriber model.
The class where the event occurs means that an event is triggered in this class, but this class does not know which object or method will add and process the event it triggers. What is needed is a medium between the sender and the receiver.
This medium is delegate in the .NET Framework.
In the event receiving and processing class, we need to have a method to handle events.
public class A { public delegate void EventHandler(object sender); public event EventHandler a; public void Run() { ("Trigger an event."); a(this); } } class B { public B(A a) { += new (); } private void b(object sender) { ("Received and handled an event." ); (); } }
Differences between event and delegate
Event is a special delegate. Both public types, for delegate, in addition to delegate, we can not only use the operator symbols of += and -=, but also call them at any time when delegate is defined; but for event, only the operator symbols of += and -= can be used outside the class, and cannot be called, that is, event turns its own invoke function and the function called parentheses into a private function that owns this event class.
event
Events can only be called by this class, and other events even derived classes of this class cannot do so. If you have to call events inside the class, you can declare a method first and call event in the method.
Action
Action is a generic delegate, which is implemented internally using delegate. When the parameters defined by ordinary delegate are the same as the number and type of Action, the functions implemented by both are the same. It’s just that the Action method is more concise and standardized.
A more important difference between Action and delegate is generics, that is, Action uses generics + delegates internally, and the number of parameters of generic methods can be extended to 16. The internal code of Action defined in Microsoft.net corefx is as follows:
namespace System { [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); [("", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.