A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method. This way, the method is dynamically assigned to the parameter, which can be avoided inProgramA large number of IF-else (switch) statements are used, and the program has better scalability.
A delegate is a reference type, so it has the connectivity of the reference type. It saves not actual values, but references to objects stored in managed heap. What references does it save? The delegate stores references to the function.
Use the delegate keyword in C # To define the delegate, and then use the familiar function call syntax to call the delegate.
Delegation is the basis of events.
The delegate is similar to the function pointer in C ++. However, the delegate type is safe and reliable.
The delegate allows passing methods as parameters.
A delegate can be used to define a callback method.
The delegate can be linked together. For example, multiple methods can be called for an event.
The method does not need to be exactly matched with the delegate signature.
There are three steps to define and use delegation: Declaration, instantiation, and call.