Delegate reference parameter example

Source: Internet
Author: User

For me, delegation has always been something I have never mastered. It is widely used in many ways. I remember it was first introduced when I learned the thread, in my later studies, I had a question about how to handle parameters when registering multiple events and passing parameters. Of course, the actual results are generally guessed by people who have learned some points, but after all, it is speculation that they have never been tested and used, and they are always timid. Below is an example of mine.

Ps: the order of broadcast is added according to the order of broadcast. The next method is implemented only after the original parameters are modified for each broadcast. The following is the result, if needed, you can format the time, or sleep for one second each time. The time is different. It is troublesome to change the image ....

Code
Delegate void strFactory (ref string str );
Class Program
{
Static void Main (string [] args)
{
StrFactory fac = new strFactory (test );
Fac + = test;
String str = "startTime :";
Fac + = test;
Fac (ref str );
Console. WriteLine (str );
}
Static void test (ref string str)
{
Str + = string. Format ("{0} \ t", DateTime. Now. ToLocalTime (). ToString ());
}
}

Result:

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.