Tips (7): Agent & event, and Agent

Source: Internet
Author: User

Tips (7): Agent & event, and Agent

Proxy delegate:

Object reference points to a specific type of object.

The proxy points to a specific type of method.

Proxy Step 4:

 

Class Program

{

// Define the frist proxy

Public delegate void first (int I );

// Main function, main Entry

Static void Main (string [] args)

{

// Create a first-type reference

First MyDelegate = null;

// Create a proxy reference pointing to the show Method

MyDelegate + = new first (show );

// Call the show method through proxy reference

MyDelegate (666 );

Console. ReadKey ();

}

// Show Method

Public static void show (int I)

{

Console. WriteLine (I. ToString ());

}

}

Class Program

{

// Define the frist proxy

Public delegate void first (int I );

// Main function, main Entry

Static void Main (string [] args)

{

// Create a first-type reference

First MyDelegate = null;

// Create a proxy reference pointing to the show Method

MyDelegate + = new first (show );

// Call the show method through proxy reference

Diao( 666, MyDelegate );

Console. ReadKey ();

}

// Show Method

Public static void show (int I)

{

Console. WriteLine (I. ToString ());

}

//

Public static void diao (int I, first dele)

{

Dele (I );

}

}

 

Event:

// Define the EventDelegate proxy

Public delegate void EventDelegate ();

Class Program

{

// Main function, main Entry

Static void Main (string [] args)

{

// Instantiate ClockTimer

ClockTimer clockTimer = new ClockTimer ();

// Add the OnClockTimer method to MyEvent

ClockTimer. MyEvent + = new EventDelegate (OnClockTimer );

// Execute the show method of the clickTimer object

ClockTimer. show ();

Console. ReadLine ();

}

// Acceptance Method

Public static void OnClockTimer ()

{

Console. WriteLine ("receive clock event ");

}

}

// Event generation class

Public class ClockTimer

{

// Define an event)

Public event EventDelegate MyEvent;

// Event Generation Method

Public void show ()

{

For (int I = 0; I <1000; I ++)

{

// Generate an event

MyEvent ();

// Sleep for 1 second

Thread. Sleep (1000); // System. Threading;

}

}

}

Masters, the above is my understanding of event and delegate. If something is wrong, please help me to point it out. Thank you!

Related Article

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.