In the past, I seldom came into contact with delegation. I made custom controls over the past two days and found that delegation was very useful. So I reviewed the delegation again and sorted it out for future review.
In my understanding, delegation is also a class, which is a special data type. Because the delegated storage references methods. I don't know if you can understand this sentence. For example, if I want to train on a business trip, but I don't know which trains I have, so I entered "train (2 persons)" in the travel application form )". After seeing this application form, the conductor will set a train ticket, and I don't have to worry about details. This is a simple commission. I entrust the booking clerk to set a train ticket, and I only need to make a request (2 persons.
Through the above example, I don't know if you have a concept. The following is an example.
Using system;
Namespace delegetetest
{
Class delegeteclass
{
Public Delegate void fhandler (int A); // key-this row can be viewed as a class declaration.
Public fhandler F0; // instantiate a delegate. In this case, f0 is equivalent to a method.
Public void D (int A, int B)
{
Int c = A + B;
F0 (c); // process the delegated instance
}
}
Class Test
{
Public void output (INT Mun)
{
System. Console. writeline ("{0}", Mun );
}
[Stathread]
Static void main (string [] ARGs)
{
Test T = new test ();
Delegeteclass Dc = new delegeteclass (); // defines the delegeteclass class instance
DC. f0 = new delegetetest. delegeteclass. fhandler (T. Output); // instance Initialization
DC. D (2, 3 );
}
}
}
The above example shows that public delegate void fhandler (int A); is actually equivalent to a class.
When DC. f0 = new delegetetest. delegeteclass. fhandler (T. Output); // Initialization is performed, it is equivalent to executing the T. Output Method in the F0 method.
The parameters and return values of the output method must be the same as those of fhandler.