C # event mechanism

xiaoxiao2021-03-06  68

The event mechanism of the C # is based on the delegate implementation. Implement an event, first define a delegate type class1 {public delegate void mydelegate (MyEventargs e); public event mydelegate event1;} then we can use = and - = registration, remove event class1.event1 = New myDelegate (new myeventargs ()); class1 {public fireEvent () {if (event1! = null) // Best of event1 () when incident in Class1 ();}} If you don't want to declare your entrustment type, you can Use System.EventHandler's entrustment type Public Delegate Void EventHandler (Object Sender, Eventargs E); where Eventargs transfers additional parameters. You can implement your own Eventargs class class myeventargs: Eventargs {} class name is best ended at Eventargs, this is if the default rule is called, if you do not want to use parameters to pass EventArgs.empty

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

New Post(0)