Event experiment procedure

xiaoxiao2021-03-06  52

// This program counts from 0 to 100, and the even number generates events.

namespace myevent {public delegate int EvenNumberHandler (int Number); int type digital-to delegate class Counter // declare counter classes {public event EvenNumberHandler OnEventNumber // declare a parameter is no return value; // declare a type of event delegate EvenNumberHandler

Public counter () {oneventnumber = null;}

Public void countto100 () // Public method, count {Int Temp; int currentnumber;

for (currentnumber = 0; currentnumber <= 100; currentnumber ) {if (currentnumber% 2 == 0) {if (OnEventNumber = null!) {temp = OnEventNumber (currentnumber); Console.WriteLine (temp);}}}} }

/ * Class EventNumberHandleclass {public void EventNumberFound (int eventnumber) {console.write (EventNumber ");}} * /

Class MainClass {///

/// The primary entry point of the application. /// [stathread] static void main (String [] args) {counter mycounter = new counter (); // EventNumberHandleclass myeventnumberhandle = new eventnumberhandleclass ();

//Mycounter.onneventnumber =new evennumberhandler (myEventnumberhandle.eventnumberfound);

Mycounter.oneventnumber = new evennumberhandler (mycounter_oneventnumber); // 1. Time the mycounter_oneventnumber function is the process of the OneventNumber event, that is, "Install" to the event! // 2. Here, the call to the event function is not necessary for the parameter table because there is a delegate existence, the entrustment is equivalent to the function pointer in C , which points to // is the first address of the function, so does not write the parameter table

Mycounter.countto100 (); console.readline ();

Private static int mycounter_oneventNumber // Event function (Processing) // The type and parameter type of the return value of this function is consistent with the delegate decision, must be uniform, must be uniformly {Return Number;}}} / / Since this program, it can be seen that the processing of the event can be a member function in the class, or a separate static function.

转载请注明原文地址:https://www.9cbs.com/read-87944.html

New Post(0)