C # delegates are similar to C + + function pointers, but delegates are type-safe, meaning that pointers always point to valid functions. There are two main ways to use delegates: Back harmonic events.
A delegate as the callback function
You can use a callback function when you need to pass a function pointer to a function and then call the function through a function pointer. Callback functions are often used for asynchronous programming, and if the method being invoked is time-consuming, you can put this method on a separate thread execution, while the function pointer is given to the callback function, and then the callback function is called at the end. This makes it unnecessary for callers to be blocked by waiting for a time-consuming method to execute.
A) for example, there is a database management class Dbmanager, which tracks all active database connection DbConnection, and through the EnumConnections () method can traverse the output of these connections. Assuming that the Dbmanager is running on a remote server, it is best to execute the EnumConnections method asynchronously and receive the result using a callback function.
The DBConnection class is as follows:
The DBManager class is as follows:
The delegate Enumconnectionscallback is declared here, the method signature has no return value, there is a dbconnection parameter, and when the EnumConnections method is called, Pass a function pointer of type Enumconnectionscallback.
The calling code is then written as follows:
The function pointer points to the Activeconnectionscallback method, which has the same signature as the defined delegate.
b) each call to the EnumConnections method needs to instantiate the Enumconnectionscallback, which is inconvenient to use, then you can modify the Mycallback as static, convenient for each direct use.
c) With regard to the naming conventions of callback functions, the general practice is to end with callback, but sometimes the delegate itself often ends up with callback, which requires attention to distinguish between the two, because the callback function belongs to the method, and the delegate belongs to the class, and if they are confused, it causes a compilation error.
D) the initialization of a delegate is often time-consuming and resource-intensive, and it is not guaranteed to be used after initialization, so it can delay initialization and initialize only before it is started, using read-only properties to achieve this idea
e) What to do if you need to use multiple delegates at the same time, such as having an inventory management class once the number of items is below the alert value, a log is recorded on the one hand and an email is sent to the person concerned. The main change is where the delegate is initialized, and the two delegates are added together to assign a value to callback.
and the order in which the two callbacks are executed is in the order of addition. You can also use a minus sign to dismiss a callback.
f) There are many occasions when Windows systems use events such as Message Queuing, user interaction, and so on. Events in C # follow the publish-subscribe (Observer) pattern, and a message source can be subscribed to by multiple objects. The event is a special delegate, which must have two arguments, the first parameter is the publisher or the event's trigger, and the second argument is the object that derives from EventArgs.
Learning materials: Inside C # by Tom Archer
Inside C # notes (12) Delegates and events