Delegation should be part of advanced programming. I have searched for a long time. Most people don’t seem to understand it at all. It is just copying other people’s things. The key parts are very general. From my understanding, I will simply share with you. Let’s talk about it, it’s not too professional, but you should be able to understand it!
------------------------------------------------ ----------------
A delegate is actually a class in the net framework, so when defining it, you can find a place to define it, such as in other classes You can define it inside or directly under the namespace; in addition, since it is also a class, you have to instantiate it when using it, for example:
delegate int MyDelegate(int i);
//The above sentence is to declare a delegate, in which the so-called signature on the Internet refers to the return type int and parameter int i; after this delegate is declared, it can only be bound to a method with the same signature, for example:
//MyMethod is the method you want to call later. Its signature must be consistent with that of the delegate, that is, it has the same return type and parameters
public int myMethod(int j) p>
{
return j;
}
The method can be called directly where the delegate is instantiated
MyDelegate MyD=new MyDelegate("myMethod"); //Instantiation
MyD(5); //What is actually called here is the meMethod method, which returns 6;
---- -------------------------------------------------- --------
Can you roughly understand the use of program structure?
As for your question, where and when should delegation be used?
First of all, in the net framework, delegation is often bound to events, so it is the basis for implementing the event processing mechanism in the framework. The specific use cannot be explained clearly in a few simple sentences.
NET has its own specifications. For example, event parameters must inherit the EventArgs class, events mostly end with EventHandler, etc. If you want to study in depth, you can check the relevant information in the msdn document library to learn.
If you write a framework project yourself, you can also have your own specifications. For example, you can use delegation when you want to dynamically execute a method; a simple example:
public int myMethod(int j){}
The parameters of this method are of int type, so you can only pass int row parameters. If you change int to MyDelegate: public int meMethod(MyDelegate j) {}; //MyD is the delegate we defined above
In this way, you can pass a method with the same signature as the delegate to this myMethod method. Due to time constraints, it is not too detailed. If If you have any questions, you can discuss them together through baidu HI, or you can leave me a message.
It’s all handwritten, please read it carefully, I hope it can help you and learn from each other!
In addition, many textbooks compare delegation to the post office. This understanding is quite vivid, that is, asking the post office to help us do something, and then feedback to us relevant information. Delegation and event binding are basically In this way, we can subscribe to the information we are interested in through delegation, etc.