Callback functions are an important part of Windows programming. If you have a C or C++ programming background, you have used callbacks in many Windows APIs. After adding the AddressOf keyword to VB, developers and developers can take advantage of APIs that were once restricted. Callback functions are actually pointers to method calls, also known as function pointers, which are a very powerful programming feature. .NET implements the concept of function pointers in the form of delegates. What's special about them is that, unlike C function pointers, .NET delegates are type-safe. This shows that the function pointer in C is just a pointer to a storage unit. We cannot tell what this pointer actually points to, let alone the parameters and return types. As described in this chapter, .NET delegates operate as a type-safe operation. Below we will learn how .NET uses delegates as a way to implement events.
1. Entrustment
When you want to pass a method to another method, you need to use a delegate. To understand what they mean, take a look at the following code:
int i = int.Parse("99");
We are accustomed to passing data as parameters to methods, as shown in the example above. So, passing another method to a method sounds a bit strange. Sometimes the operation performed by a method is not performed on the data, but on another method, which is more complicated. We don't know what the second method is at compile time. This information can only be obtained at runtime, so we need to pass the second method as a parameter to the first method. This sounds confusing. Here are some examples: To explain:
1. Start threads-----In C#, you can tell the computer to run certain new execution sequences in parallel. This sequence is called a thread. Use the Start() method on an instance of accumulated System.Threading.Thread to start executing a thread. If you want to tell the computer to start a new sequence of execution, you must indicate where the sequence is to be executed. The computer must be provided with the details of the method to start execution, i.e. the Thread.Start() method must take a parameter that defines the method to be called by the thread.
2. General library classes------There are many libraries that contain code to perform various standard tasks. These libraries are usually self-contained. In this way, when writing the library, you will know how to perform the task. But sometimes tasks also contain subtasks. Only the client code using the library knows how to perform these subtasks. For example, write a class that takes an array of objects and sorts them in ascending order. However, part of the sorting process involves reusing two objects in the array, comparing them to see which one should come first. If you were writing a class that had to be able to sort any array of objects, you couldn't tell the computer in advance how the objects should be compared. Client code that handles arrays of objects in a class must also tell the class how to compare the objects to be sorted. In other words, the client code must pass certain details to the class that will make this appropriate.
3. Events ------ generally notify the code of what events have occurred. GUI programming is mainly about handling events. When an event occurs, the runtime needs to know which method should be executed. This requires passing the method that handles the time as a parameter of the delegate. These will be discussed later.
Previously we established the rule that method names are sometimes passed as arguments to other methods. It is necessary to indicate below how to complete this process. The simplest way is to pass the method name as a parameter. For example, in the previous thread example, assume that a new thread is started and there is a method called EntryPoint(), which is where the thread starts running.
void EntryPoint()
{
//do whatever the new thread needs to do
}
You can also use the following code to start executing a new thread:
Thread NewThread = new Thread();
Thread.Start(EntryPoint); //WRONG
In fact, this is a very simple way, which is the way used in some languages such as C and C++ (in C and C++, the parameter EntryPoint is a function pointer).
But this direct approach can cause some problems, such as type safety. When doing object-oriented programming, methods rarely exist in isolation. They usually need to be associated with a class instance before being called. And this method does not take this issue into account. So the .NET Framework syntactically does not allow this direct approach. If you want to pass a method, you must encapsulate the details of the method in a new type of object, a delegate. Delegates are just a special type of object. The special thing is that all the objects we defined before contain data, while delegates only contain the details of the methods.
-