. Net.

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.