C # delegation,
I have been writing some relatively basic C # code before. Now I have been working on a project for a while and have encountered more troublesome problems, such as the delegation and events to be discussed today, this is part of C # advanced article. Now I will share with you what I learned these days and what I understand. Correct the error.
Delegate
As the name implies, a delegate is similar to an intermediary. After you build it, it is always there, and then you can let it do different things. In essence, a delegate is a tool for doing things. However, it should be noted that all tasks can be done by the delegate. These tasks must have the same features, such as the same parameters as the delegate. Okay, let's talk about it in Demo.
Create a console Project
Then, we first define a delegate in the project. In fact, the delegate is also considered to be a class, which belongs to the reference type (iint and other belong to the value type, and the bucket is a stack, the reference type bucket is heap), so we can directly process it like a class when processing the delegate, but the difference when declaring is that we need to write parameters.
Then we create a new method, which is what we need to delegate, that is, what this intermediary does. Note that we need to ensure that the parameter types and numbers of this method are consistent with those of the delegate, because you cannot allow a house-selling intermediary to help you buy a car, right!
Finally, let's use the delegate. As I mentioned earlier, the delegate is almost similar to a class. We have to instantiate it if we want to use the delegate, at this time, we have to tell it what we need this intermediary to do.
Note that the passed parameter is a method instead of an int double type.This is also the essence of delegation. Then we can see that in the code, I then used the delegate to pass
Int type parameter, because we previously specified a required parameter when defining the delegate, the type is int.
Now let's take a look at the final output result and verify it !!!
Now we know the most basic commission, and then let's take a look at the multi-channel broadcast commission. In fact, this intermediary can help us do multiple things at a time. If we can't do more than one thing at a time, what should we do with this intermediary? It would be better to simply do it on our own.
Let's add two things first.
Now let's tell the intermediary to do these two things.
We use the + = method to add things to be done to the delegate. At this time, there is a linked list in the delegate, which can be added and deleted continuously (deleted as-= ), similar to list operations.
Now let's take a look at the running results.
Let's summarize the delegation.
The delegation mainly consists of three steps:
1. Define a delegate
Ii. What needs to be done
3. Pass the event to the delegate through Parameters
Finally, use the delegate !!!
PS: today, due to not much time, we can only write and delegate for the time being. If you want to learn more about delegate details, you can go to other blogs in the garden and check out many excellent posts.
Next time, I will share with you my understanding of the event on the basis of delegation.